Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to show a list of posts that are related to category X and tag Y. I've tried the following code:

$args = array(
    'posts_per_page' => 4,
    'tag_id' => $tag_id,
    'cat' => $cat_id,
);
query_posts($args);

but it doesn't work correctly and returns all the posts in the co\ategory.

Would love to hear any insight you might have

share|improve this question
    
I think with query_posts() you can only make use of category or tag. I'm not sure, but maybe the use of the function is limited to that which would mean that this is correctly working but it doesn't do what you want to do it. –  hakre Nov 17 '10 at 23:40
add comment

5 Answers 5

up vote 4 down vote accepted

Edit: See below for proper way to query category and tag intersections.

global $wp_query;
        $args = array(
        'category__and' => 'category', 
        'tag__in' => 'post_tag', //must use tag id for this field
        'posts_per_page' => -1); //get all posts

$posts = get_posts($args);
        foreach ($posts as $post) :
  //do stuff 
     endforeach;
share|improve this answer
add comment

See: http://wordpress.org/support/topic/query_posts-in-category-with-tag

share|improve this answer
1  
A link only answer is a bad one. It'd be invalid while the link becomes invalid. Try editing your Answer and elaborate what the plugin is for and how it can solve the issue. –  Mayeenul Islam Jun 9 at 3:57
    
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  TheDeadMedic Jun 9 at 8:13
add comment

I think this is bug in WordPress that has been commented on elsewhere, try using the name of the tag rather than the ID then it should work:

$args = array(
    'posts_per_page' => 3,
    'tag' => 'review',
    'cat' => 9,
);
query_posts($args);

Let us know how you get on, not sure what happens with tags with multiple words in the name.

share|improve this answer
add comment

I stumbled into this same issue and resolved it by making a MySQL request .

in short : get_post($args) will return you posts who have the category=MyCategory OR the tag=MyTag.

what you want is to change your OR to AND .

my logic was to go straight with a MySQL Query:

  • Query 1 = Select all the posts who has the category MyCat
  • Query 2 = Select all the posts who has the tag MyTag
  • FinalLY : Select all the posts who are in Query 1 AND Query 2 .

I used wpdb instead of query_post();

A bit of code (returning published posts with category MyCat and tag MyTag ):

    $query_byTag="
            SELECT wp_posts.ID
            FROM wp_posts, wp_term_relationships, wp_terms
            WHERE wp_posts.ID = wp_term_relationships.object_id
            AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
            AND wp_terms.name = 'MyTag'";

    $query_byCat="
            SELECT wp_posts.ID
            FROM wp_posts, wp_term_relationships, wp_terms
            WHERE wp_posts.ID = wp_term_relationships.object_id
            AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
            AND wp_terms.name = 'MyCat'";

$query ="
            SELECT      wp_posts.post_title AS title , 
                        wp_posts.post_content AS content,
                        wp_posts.post_date AS blogdate 
            FROM wp_posts
            WHERE wp_posts.post_status = 'publish'
            AND wp_posts.ID IN (".$query_byTag.")
            AND wp_posts.ID IN (".$query_byCat.")
            ORDER BY wp_posts.post_date DESC ";

$result= $wpdb->get_results($query);

This is a dirty way to do it but I hope it helps =)

share|improve this answer
1  
This is much more easily accomplished with WP_Query and a tax_query AND relationship, no need for raw SQL. –  Milo Jun 8 at 19:10
add comment

I believe it will work if you remove the offending comma at the end of the array...

   $args = array(
        'posts_per_page' => 4,
        'tag_id' => $tag_id,
        'cat' => $cat_id
    );
    query_posts($args);
share|improve this answer
1  
nope, it just returns all the posts in the category. –  hannit cohen Nov 17 '10 at 22:55
1  
The comma is not a problem, infact it helps when you duplicate lines to enter additional data. The comma can be written or not, it changes nothing. –  hakre Nov 17 '10 at 23:23
1  
Indeed, whilst "odd" to say the least, it is apparently valid to leave that trailing comma in... Ref: php.net/manual/en/function.array.php (i've just learnt that, ^_^)... –  t31os Nov 18 '10 at 23:42
    
The last comma is actually recommended to be there, if you want to adhere to the WordPress Coding Standard –  windyjonas Nov 12 '13 at 11:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.