Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have three separate WP_Queries on a page. Each of them return custom post type items that have a certain checkbox checked and then display one of those randomly. The checkbox has been added using Custom Metaboxes and Fields for WordPress.

<?php
$front1_args = array(
    'post_type' => 'work-item',
    'meta_key' => '_cmb_work_front_large',
    'meta_value_num' => 1,
    'orderby' => 'rand',
    'posts_per_page' => '1'
);
$front1_query = new WP_Query($front1_args);
while ($front1_query->have_posts()) : $front1_query->the_post();    
?>
<div> 
  post content comes here
</div>
<?php 
endwhile; 
wp_reset_postdata();
?>

However unpicking the checkbox on a certain post sometimes has no effect. The query still returns all items that at some point have had the checkbox checked regardless if they have it now. Setting cache_results, update_post_meta_cache and update_post_term_cache to false has no effect.

Any ideas what might cause this? Is this a cache issue at all, or is CMB maybe at fault?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Found a fix. Apparently CMB doesn't use integers while saving values to database from custom checkboxes, but rather adds an on string when a checkbox is checked.

Also when a previously checked checkbox is unchecked, the corresponding row isn't removed from wp_postmeta table, but remains there with an empty value.

So this will return all posts that at some point have had the checkbox active:

'meta_key' => '_cmb_work_front_large',
'meta_value_num' => 1

And this will show only the active ones:

'meta_key' => '_cmb_work_front_large',
'meta_value' => 'on'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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