• Hi,

    Forgive my question if it’s covered elsewhere, but I cannot find a solution to it, so far

    Let’s say I have two categories: A and B
    Let’s also say that I have 3 items in category A and 10 items in category B … because I do.
    Finally let’s say I have 2 items that are assigned to both categories A and B (also true)

    I can get all items from category A or all items from category B or all items from both categories A & B no problem. I’ve been able to accomplish this by using:
    query_posts( array ( 'category_name' => $category ) )
    OR

    $both = (A, B)
    query_posts( array ( 'category__and' => $both ) )

    Here’s where I’m having trouble:
    I want to get all 3 items that are in category A and both items that are in category B … but NOT the 10 items that are in only category B — resulting in 5 items returned. As a converse, I also want to be able to get all 10 items in category B as well as both items that are in category A & B … but NOT the 3 items that are in only category A — resulting in 12 items returned.

    How can I accomplish this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Perhaps I am misunderstanding what you want.

    For the first case, you say

    I want to get all 3 items that are in category A and both items that are in category B … resulting in 5 items returned

    It seems to me that a query for category A will return 5 items as requested.

    Similarly, a query for category B should return the 12 items for the second case.

    Thread Starter dacarr

    (@dacarr)

    That’s what I thought too, but when I query category A I get 5 items returned; however when I query category B only 10 items are returned, not 12, so something is obviously not right … not sure what though.

    It could be that there is something filtering the query. You can see the actual query used by adding this just after the query_posts():

    global $wp_query;
    print_r('<p>REQUEST:');print_r($wp_query->request);print_r('</p>');

    Compare the query for A to the query for B and see if that helps.

    Thread Starter dacarr

    (@dacarr)

    Both queries returned the same response:

    REQUEST: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) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (4) ) 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 ASC LIMIT 0, 10

    The only difference was the category ID returned in wp_term_relationships.term_taxonomy_id IN () … in one case, it was “3” which is the category ID corresponding to “A” in the initial post and “4” which is the category ID corresponding to “B” in the initial post.

    Is it possible that the last line, “LIMIT 0, 10” is causing the limit of 10 items returned in the original post?

    That is the reason! Add ‘posts_per_page’ => -1 to your query to return all posta.

    Thread Starter dacarr

    (@dacarr)

    Excellent!! That did the trick!

    Thanks for all your help

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Getting posts’ is closed to new replies.