Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Cliff Tam
Cliff Tam
2,948 Points

Check meta data before it is published

I am using a church theme and a custom post with its own meta data.

What i want to do is that if a sermon post is published, WP will check whether someone included a video URL. If they do, check the Video Sermon category. If they do not, do nothing.

My problem is that this code only works when the person click on Save two times. I suspect it is because my codes is pulling data from the database. Since the first time someone posted, the data is not in the DB yet. Can someone showed me how to test the meta data before it is published or written in the DB?

Here's my code:

//Sermon post include Sermons category function add_sermons_automatically($post_ID) {

//check video

$video_info = get_post_meta('imic_vimeo_video', $post_ID);
 $video_info .= get_post_meta('imic_mp4_video', $post_ID);
 $video_info .= get_post_meta('imic_webm_video', $post_ID);
 $video_info .= get_post_meta('imic_ogg_video', $post_ID);
 $video_info .= get_post_meta('imic_youtube_video', $post_ID);

if (!empty($video_info)) {
    $sermons_cat = array(28);
    wp_set_object_terms( $post_ID, $sermons_cat, 'sermon-category');

} add_action('publish_sermon', 'add_sermons_automatically');

Thanks, Cliff