0

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.

  • Are you sure you have 15 posts in those categories that have the featured-checkbox meta value equal to no. What if you set meta_compare to != and value to yes? – czerspalace Dec 21 '16 at 17:12
  • Same result; same 7 posts displayed. Only 2 posts out of 250+ have the meta value 'yes'. – markratledge Dec 21 '16 at 17:20
  • Do the posts really have the value 'no' or are they maybe empty? – JHoffmann Dec 21 '16 at 17:45
  • They are def. yes and no. – markratledge Dec 21 '16 at 17:46
  • What is the SQL query if you var_dump($the_query->request); – czerspalace Dec 21 '16 at 18:29
-1

try to add wp_reset_query() in the end of your query

  • Makes no difference; wp_reset_query is for the subsequent loop, not the previous loop – markratledge Dec 21 '16 at 18:18

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.