• Hello,

    I have my own theme run on WordPress 6.1.1. and I think just after update to 6.1.1. (however I am not sure), I’ve noticed strange error.

    I have piece of code that gets posts from the category slug “blog-en” plus some more arguments for the WP_Query – here is the code:

    $args = array(
    
                        'category_name' => 'blog-en',
    
                        'post_type' => 'post',
    
                        #'category__in' => 3,
    
                        'order' => 'DESC',
    
                        'orderby' => 'date',
    
                        'posts_per_page' => '4',
    
                      );
    
                      do_action('qm/info', $args);
    
                      #wp_reset_postdata();
    
                      $htPosts = new WP_Query;
    
                      $htPosts->query($args);
    
                      do_action('qm/debug', $htPosts->request);
    
                      do_action('qm/debug', $htPosts);

    As you can see I am checking the SQL request in the Query Monitor and it gives me the SQL:

    SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID
    					FROM wp_posts  LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    					WHERE 1=1  AND ( 
      wp_term_relationships.term_taxonomy_id IN (369) 
      AND 
      0 = 1
    ) AND ((wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' 
    OR wp_posts.post_status = 'private')))
    					GROUP BY wp_posts.ID
    					ORDER BY wp_posts.post_date DESC
    					LIMIT 0, 4

    In bold you can see 0 = 1 in the query, and this is wired to me since if I change the php code and choose the category__in => 3 (this is the ID of category_name = blog-en), the query looks like:

    SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID
    					FROM wp_posts  LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)  LEFT JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)
    					WHERE 1=1  AND ( 
      wp_term_relationships.term_taxonomy_id IN (369) 
      AND 
      tt1.term_taxonomy_id IN (3)
    ) AND ((wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' 
    OR wp_posts.post_status = 'private')))
    					GROUP BY wp_posts.ID
    					ORDER BY wp_posts.post_date DESC
    					LIMIT 0, 4

    where there is no magical 0 = 1 and I’ve got proper results on my page.

    What is wrong with my code? Maybe this is some issue with WordPress 6.1.1?

    Could somebody helps me?

    Best regards,

    ZiemoT

    • This topic was modified 1 year, 8 months ago by ziemot.
Viewing 1 replies (of 1 total)
  • Moderator Dion Hulse

    (@dd32)

    Meta Developer

    Hi @ziemot,

    You’ll need to test this without other plugins disabled I believe, as it looks like something else might be affecting the query. You could also try adding 'suppress_filters' => true to your query.

    I say that as both of your queries show that there’s two taxonomy queries occurring, not just the singular category query. You can see that by the part of the SQL in question having two table joins wp_term_relationships and tt1 – A straight category query like yours should have a singular join.

    You might also be able to inspect the contents of $htPosts->tax_query to find the second query.

Viewing 1 replies (of 1 total)
  • The topic ‘WP_Query args category_name issue’ is closed to new replies.