What I want to do is display my latest post's content on a custom template page, ONLY if a meta box is filled in with the right word. In other words, if the post has a meta box named, "FEATURED" with a value of "YES", then I would like to show the post on my custom template page. If the "FEATURED" meta box says, "NO", then I do not want the post to show up. With some previous help, I am currently using the following to show the latest post on my homepage:

$my_most_recent_post = get_posts( 'numberposts=1' );
$my_most_recent_post_id = $my_most_recent_post[0]->ID;

My homepage will show the latest post no matter what, but on this particular page, I only want it to display conditionally depending on the meta box. So can I take that code and make it work in a conditional statement?

2 Answers 2


You're speaking about custom field content and not meta box content - the meta box is the wrapper box where custom fields are displayed.

For the custom field display, you can create a WP_Query with a meta condition, i.e. adding a specific condition whether a custom field is set and with a specific value. @Rarst has given a similar example here.

See the WP_Query meta queries page for more details.

  • Thank you for clarifying that. I knew what I meant, but said it wrong, haha! Well, I am trying to do your suggestion, and it will not work. I have tried it a few different ways, but no luck. Very weird. If my post has a custom field name of "Featured", with a value of "yes", then I would think that $the_query = new WP_Query(array( 'meta_key' => 'Featured', 'meta_value' => 'yes' )); would work, right? I tried it with both "$the_query" and just "$query" with no luck. Nov 11, 2012 at 21:05
  • it really does depend on the context and the meta queries could be confusing sometimes, if you post the full snippet, we could help more Nov 11, 2012 at 21:31
  • Thank you. Well basically this is what I'm working with: pastebin.com/7skuybvL minus some content in between the header and footer, I'm trying to display my latest blog post content(ideally, all of it...title, featured image, and content etc) in this template page, as long as the post's custom field of "Featured" has a value of "yes" in it. Nov 11, 2012 at 21:54
  • Try the meta_query subarray syntax as well, for instance - wordpress.stackexchange.com/a/44800/11506 , see if that would work? Nov 11, 2012 at 22:18
  • Hi Mario, is this what you mean? pastebin.com/BtXk00gy I'm unsure what to put in the "post type" area. Is this talking about custom post types? I'm also unsure about the "compare" part of this code. Nov 11, 2012 at 22:36

You could just do this:

  if (have_posts()):
    while (have_posts()): the_post();
      $featured = get_post_meta($post->ID, 'featured', true);
      // Or whatever the meta value is called
      if ($featured == 'yes'):
        the_title('<h1>', '</h1>');
        // etc;
  • Thanks for your reply! This option is more familiar to me, but will not work either. I'm trying to use it within a custom template page. It seems like this should work with no issues! Nov 11, 2012 at 21:08
  • If that's a custom page, a custom query should be build naturally as mentioned above. Also, this approach has a drawback - it loops for X posts and displays only the featured posts, and this could go from 0 to N depending on whether there are any featured or not (unreliable end result count). Nov 11, 2012 at 21:33
  • Thank you Mario. I'm slowly trying to wrap my head around php. Nov 11, 2012 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.