Display posts from x cat and y tag

Is it possible to fetch posts matching categorie x “AND” tag y?

Read the docs, it seems you can do:

Read More
query_posts('tag=bread,baking');

or

query_posts('cat=2,6,17,38');

… is it possible to use both cat and tag simultaneously?

Related posts

Leave a Reply

3 comments

  1. I am not a WordPress expert, but what I see from looking up that function, is that you should be able to use this notation in order to query against both at the same time.

    query_posts('tag=bread,baking&cat=2,6,17,38');
    
  2. This is taken from another question that I had answered previously and it was tested and worked properly, so here we go. You can manually query your database using the following:

    SELECT *  
    FROM wp_term_taxonomy AS cat_term_taxonomy 
    INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id 
    INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id 
    INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID 
    WHERE cat_posts.post_status = 'publish' AND cat_posts.post_type = 'post' AND cat_terms.term_id = '13,26,45,89,117'
    

    All that you would do is that you would provide the termID for each of the tags/categories that you want to find.

    I’m not sure if it would work or not, but technically tags and categories are within the same table. So, I would think that if you provide the tagID within the cat= parameter it might work, I don’t have a chance to test it currently, but definitely worth a try.

  3. Yes, you can. I recently had to show all future posts on a page of my wordpress log and I just used:

    query_posts($query_string . '&post_status=future,publish');
    

    which worked flawlessly.