• This might be a dumb question, but I haven’t spent lots of time trying to find the answer in the documentation and the forums, and can’t get anything.

    How do you query blog posts that are in BOTH category A and category B?

    If I use

    https://www.mysite.com/index.php?category_name=A&category_name=B

    I get posts that are EITHER in category A OR in category B. But I only want to see posts that are in BOTH.

    Is this possible within a URI? Or otherwise?

    TIA

Viewing 9 replies - 1 through 9 (of 9 total)
  • WordPress out of the box? No. Most certainly not by doubling (tripling, etc.) a query, as the last will merely override previous queries of the same type (pretty much out of WP’s hands).

    However, one *might* be able to collect categories as an array in a query and pass them to a modified method that calls up the posts. But I’m not well versed in this area of WordPress to suggest a solution.

    Thread Starter latext

    (@latext)

    Thanks for your reply. I find it hard to believe that this cannot be done without advanced scripting! I am afraid I know very little about PHP and have no idea how to do this. Surely there must be an easy way… if not through a URI, then through a form? (although I’d prefer the ability to provide a direct link)

    why not force it wo happen by using this method from the template category php file:

    'function in_category($category) '

    do an if then with if post in category 1 and in category 2 then display, php is supposed to be reletively easy to figure out, (though i don’t really know it and would need to do a google search for tutorials to code it properly)

    the above function returns a true or false based on whether a post is in a given category see line 418 or so of template-functions-category.php in the includes directory

    i think you could add the if then to the index.php file inside the archives stuff and the category listing if then statement that is already there.

    funny you should even bring this topic up as i was just wondering if this was possible last night!

    Thread Starter latext

    (@latext)

    OK, I’m getting there. I’ve figured out that

    <?php if (in_category(‘1’) and in_category(‘2’)) : ?>

    will display only posts that are in BOTH category 1 and category 2.

    Now all I need to do is pass “1” and “2” as parameters in my URI, i.e. something like:

    https://www.mysite.com/index.php?category1=1&category2=2

    Is this possible?

    in_category($_GET[‘category1’])

    Thread Starter latext

    (@latext)

    Zeeg: Thanks. It’s ALMOST working. There’s one problem left: For some reason, WP only displays those posts that are in category 1 — in ADDITION to the criteria defined by the parameters. In other words, if I use

    https://www.mysite.com/index.php?category1=2&category2=3

    WP behaves as if I had requested posts that are in categories 1, 2 and 3. There’s something somewhere that makes WP think that I only want posts that meet my criteria AND are also in category 1.

    How come, and how do I eliminate this additional condition that I do not want?

    Thread Starter latext

    (@latext)

    Well, it seems to be a problem with category_cache, because I wrote a new function instead:


    function in_categorie($category) { // Check if the current post is in the given category
    global $post, $tablecategories, $tablepost2cat, $wpdb;
    $cats = '';
    $categories = $wpdb->get_results("
    SELECT category_id, cat_name, category_nicename, category_description, category_parent
    FROM $tablecategories, $tablepost2cat
    WHERE $tablepost2cat.category_id = cat_ID AND $tablepost2cat.post_id = $post->ID
    ");

    foreach ($categories as $cat) :
    $cats[] = $cat->category_id;
    endforeach;

    if ( in_array($category, $cats) )
    return true;
    else
    return false;

    }

    and it works with that new function!

    That being said, I am not really satisfied with this approach, because it effectively just filters out the posts IN THE CURRENT REQUEST that do not match the conditions. The problem with this is that there is no way to tell in advance if the page is going to contain ANY posts and then, if some posts do match the conditions, on a page-based system, that means some pages can have 3 posts while other pages might have 5 posts, etc.

    What I would really like is code that actually ONLY requests from the database those posts that do match the conditions, so that I can tell right away if there are no posts that match the conditions, and also so that I can control how many posts per page I am going to get.

    Is this possible without becoming a WordPress expert? :-/

    bump.. I’d like to know the query for getting both categoryA and categoryB too..

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to query posts that are in category A and category B?’ is closed to new replies.