WordPress.org

Ready to get started?Download WordPress

Forums

category__in doesn't play nice with others in WP_Query (4 posts)

  1. sboisvert
    Member
    Posted 4 years ago #

    While Passing Parameters in WP_Query. it seems that the 'category__in' doesn't play nice. (or something else is wonky)

    these parameters will work fine and give expected results:

    $params = array('category__not_in'=> array(4,5),'posts_per_page'=>50,'tag_slug__in'=>array('world','monde'));
    $params2 = 'cat=4&tag=world';
    $params3 = 'cat=5&tag=monde';
    $params4 = array('category__in'=> array(4,5),'posts_per_page'=>50);

    while this will give me nothing even thought it should technically (from my humble understanding) include at the very least the results of the $params2 in the above code block.

    $params5 = array('category__in'=> array(4,5),'posts_per_page'=>50,'tag_slug__in'=>array('world','monde'));

    Putting JUST category__in works fine. as my $params4 shows. Am I mis-understanding how they would interact or should I be filling a bug request?

  2. MichaelH
    Member
    Posted 4 years ago #

    I don't know if it is a bug (meaning I don't know if those are intended to work together) but it doesn't work for me either.

    Here's the SQL that generates--maybe you or someone can spot a problem.

    SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
    INNER JOIN wp_terms ON (wp_term_taxonomy.term_id = wp_terms.term_id)  WHERE 1=1  AND wp_term_taxonomy.taxonomy = 'category'
    AND wp_term_taxonomy.term_id IN ('4', '5')
    AND wp_term_taxonomy.taxonomy = 'post_tag'
    AND wp_terms.slug IN ('world', 'monde')
    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, 50
  3. sboisvert
    Member
    Posted 4 years ago #

    Thank you very Much Michael,

    I believe from looking at the Query that one would need to join twice on wp_term_taxonomy and wp_terms, using aliases so as to not clash in the where clause to be able to search for things that are 'post_tag' in one joined version of those table and using the IDs directly in the other. currently it is smooching it all in one and therefore I believe that at the very least it is unexpected behavior and might qualify as a bug.

    I believe I wouldn't have this problem if I was using IDs instead of slugs because then it wouldn't have to say: AND wp_term_taxonomy.taxonomy = 'post_tag'
    meaning that the IDs could just be put together since tags and categories grouped the same way in the db.

    -S

  4. MichaelH
    Member
    Posted 4 years ago #

    I believe I wouldn't have this problem if I was using IDs instead of slugs

    I'm not sure of that but you could check that out.

    How do I determine a Post, Page, Category, Tag, Link, Link Category, or User ID?

Topic Closed

This topic has been closed to new replies.

About this Topic