• I have come across something very odd: I am doing a custom query and I need to select posts that belong in TWO categories. So, I’m using the “category__and” query option.

    $args['category__and'] = $my_array;

    However, it was producing inaccurate results. So, I did some debugging and have printed out my SQL. Here’s an example of what’s happening.

    Desired categories = 157 and 141

    In the SQL, this is what I see:

    WHERE term_taxonomy_id IN (140,156)

    How bizarre is this? If I change the selected categories, it always actually uses the category minus 1. I’ve printed out my array right before, to make sure I’m not actually giving it the wrong numbers… but the array is fine:

    Array ( [0] => 157 [1] => 141 )

    Is this a bug with “category__and”? Am I misunderstanding how this should work? Any help would be appreciated.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter relish1227

    (@relish1227)

    Actually, it does this with cat, too! I decided to see if there was an issue with just giving the query “category__and” without a “cat” first being set. So I hardcoded my $args to include

    $args['cat'] = "157";
    $args['category__and'] = "141";

    Here’s also the output of all of my $args values:

    post_status: publish
    posts_per_page: 20
    paged: 1
    meta_key: support-file-url
    orderby: post_date,meta_value
    order: DESC,ASC
    cat: 157
    category__and: 141

    Here’s the SQL that query_posts($args) gives me:

    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) JOIN wp_icl_translations t ON wp_posts.ID = t.element_id AND t.element_type IN ('post_post','post_page') JOIN wp_icl_languages l ON t.language_code=l.code AND l.active=1 WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (156) AND ( SELECT COUNT(1) FROM wp_term_relationships WHERE term_taxonomy_id IN (140) AND object_id = wp_posts.ID ) = 1 ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') AND (wp_postmeta.meta_key = 'support-file-url' ) AND t.language_code='en' GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 20

    See the “wp_term_relationships.term_taxonomy_id IN (156)” and “term_taxonomy_id IN (140)”???? Why is it offsetting the input category values by one? I also double-checked the database to make sure that the IDs corresponded to the desired categories… and I definitely want to be searching for 157 and 141, not 156 and 140.

    Definitely seems like something is wrong here.

    Thread Starter relish1227

    (@relish1227)

    I posted more code here: https://gist.github.com/relish27/5237317

    I also just tried copying and pasting the “category__and” code from the WP
    query_posts page: http://codex.wordpress.org/Function_Reference/query_posts

    But with the IDs changed in the array:

    query_posts( array( 'category__and' => array(157,158,144), 'posts_per_page' => 2, 'orderby' => 'title', 'order' => 'DESC' ) );

    It still returns “WHERE term_taxonomy_id IN (143,156,157)” in the SQL! Am I misunderstanding how this works? I don’t understand where this offset is coming from…

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Category__And not working — selects offset of desired value’ is closed to new replies.