I'm puzzled: why does adding a meta_query
array break this WP_Query? No errors shown with debug. I don't know what else to try.
This works fine:
$args = array(
'category__in' => array( 24,420,421 ),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 15,
'posts_per_archive_page' => 15,
'nopaging' => false,
'paged' => get_query_var('page'),
'orderby' => 'date'
);
$args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$the_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$even_odd_class = $the_query->current_post % 2 == 0 ? 'even' : 'odd';
But as soon as I add the meta_query' => array
, it breaks halfway through and does not display 15 posts.
$args = array(
'category__in' => array( 24,420,421 ),
'meta_query' => array(
array(
'key' => 'featured-checkbox',
'value' => 'no', 'meta_compare' => '=' )
),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 15,
'posts_per_archive_page' => 15,
'nopaging' => false,
'paged' => get_query_var('page'),
'orderby' => 'date'
);
$args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$the_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$even_odd_class = $the_query->current_post % 2 == 0 ? 'even' : 'odd';
The (abbreviated) rest of the loop used:
the_content;
endwhile;
the_posts_pagination();
else : get_template_part( 'template-parts/content', 'none' ); endif;
• And, the values of featured-checkbox
in the custom fields viewer in WP Admin are yes
and no
.
Update: Good point re:var_dump; I got
string(562) "SELECT SQL_CALC_FOUND_ROWS wpiv_posts.ID FROM wpiv_posts LEFT JOIN wpiv_term_relationships ON (wpiv_posts.ID = wpiv_term_relationships.object_id) INNER JOIN wpiv_postmeta ON ( wpiv_posts.ID = wpiv_postmeta.post_id ) WHERE 1=1 AND ( wpiv_term_relationships.term_taxonomy_id IN (24,420,421) ) AND ( ( wpiv_postmeta.meta_key = 'featured-checkbox' AND wpiv_postmeta.meta_value = 'no' ) ) AND wpiv_posts.post_type = 'post' AND ((wpiv_posts.post_status = 'publish')) GROUP BY wpiv_posts.ID ORDER BY wpiv_posts.menu_order, wpiv_posts.post_date DESC LIMIT 0, 15"
and when I run that in the database, I get 7 posts, same as are displayed. But I have 250+ posts in those three categories, and only 2 have the meta_query
set to yes
.
featured-checkbox
meta value equal tono
. What if you set meta_compare to!=
and value toyes
?var_dump($the_query->request);