Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

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