I created a meta box with a value "home" of type checkbox, however I'm having hard times retrieving into the loop, I'm using this code into the loop:

 <?php if(get_post_meta(get_the_ID(), 'home') == '1' ) : ?> 
 <?php endif; ?>

if the checkbox is checked it should work like this, however is not... the most funny part is if I set it to '0', it displays all the other pages where is not checked.

Any guess?

Thanks in advanced


Page Options code

 $meta_boxes[] = array(
'id' => 'page-options',
'title' => __('Page Layout', 'andrewslab'),
'pages' => array('page'),
'context' => 'normal',
'priority' => 'high',
 'fields' => array(

        'name' => __('Home', 'andrewslab'),
        'id' => 'home',
        'type' => 'checkbox',
        'desc' => 'Display in <strong>home page</strong>',
        'std' => ''

Template Code

     global $wp_query;
     $args = array_merge( $wp_query->query, array( 'post_type' => 'page' , 'showposts' => '4' ) );
     query_posts( $args );

 <?php while (have_posts()) : the_post() ; $meta = get_post_meta( get_the_ID(), 'home', true ); ?>
     <?php if( checked( $meta, 1, false ) ) : ?>

     <?php endif ?>     

 <?php endwhile; ?>
 <?php wp_reset_query() ?>

Basically I need to enter into the loop only if the checkbox is checked.


You need to add the third parameter to get_post_meta(), so get_post_meta( get_the_ID(), 'home', true ) should at least get you closer. Just a note, in future, try doing a var_dump() or print_r() on the variable(s) in the condition that are evaluating in an unexpected manner, if you'd done that here you would have seen that it was outputting an array, not the int/string you were expecting :)


Checkboxes are a bit of a strange breed with respect to form data. They only get sent in the $_POST data if they are set. Your data-validation callback for saving custom post meta data should use an isset() conditional as part of checkbox validation, and then set the value accordingly.

Also, when you call get_post_meta(), the results are an array, so you'll need to look at the first value of that array. I usually use something like this:

$custom = get_post_meta(get_the_ID(), 'home');
$custom_home = ( isset( $custom[0] ? $custom[0] : false );
  • I get what your say, however this is nor working for me =(, neither the solution jared suggested, is really weird – andresmijares Jan 26 '12 at 2:32
  • Can you please post your metabox generation and validation/update code? – Chip Bennett Jan 26 '12 at 2:34
  • I just updated it, basically I need to enter into the loop only if the checkbox is checked, thanks a ton Chip – andresmijares Jan 26 '12 at 3:14
  • Where is the validation/update code? – Chip Bennett Jan 26 '12 at 3:29
  • leave it blank since it was not clear what to use, I just update the last tried – andresmijares Jan 26 '12 at 3:36

After some research I was able to solve it... looks like it understands the values as 'on' and 'off'... with this validation it works like charm

 <?php if(get_post_meta(get_the_ID(), 'home', true) == 'on') : ?>

<?php the_title() ?>

 <?php endif; ?>

Wordpress has a convenient way of doing this. There's actually a checked (be sure to read the documentation) function built in that you can use. So for your example:

$meta = get_post_meta( get_the_ID(), 'home', true );
if( checked( $meta, 1, false ) ) {
    // do stuff

And as @m0r7if3r said, you need to add true as the third argument to get_post_meta.

Not tested but should work.

  • I tried with ur suggestion, however is not working neither, I also read the article you suggested and couldn't find a solution =( – andresmijares Jan 26 '12 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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