• I’d like to be able to show posts that have a specific tag and are in a specific category, but query_posts() doesn’t seem to want to let me do that.

    I assumed that this code (or something like it) would do the trick:

    query_posts("cat=6&tag=featured&showposts=5");

    but no-dice. This just doesn’t return any posts, even though there are posts in cat 6 tagged ‘featured’. If I remove the cat= or tag= arguments, it works fine to just pull up posts tagged ‘featured’ or posts in cat ‘6’, and it would seem like I should be able to query for the intersect of those results.

    Anybody have any suggestions? Am I missing something glaringly obvious?

Viewing 15 replies - 16 through 30 (of 34 total)
  • Well to get back to the first problem…
    I was faced with the same problem, while trying to find a fix I found the following. Excluding all categories, except the one you want, will get you the desired results. For example:

    I Was trying for this:

    query_posts('cat=32&tag=hs1&showposts=5');

    And as mentioned, it doesn’t work.
    But this, excluding categories, does:

    query_posts('cat=-1,-3,-4,-5,-8,-9&tag=hs1&showposts=5');

    try:
    query_posts(‘cat=32&tag=hs1+hs1&showposts=5’);

    Yes, that works too!
    Thanks!! You saved me from a lot of excluding.

    For multiple categories (posts with ID9 _and_ 13) try:

    query_posts(array(‘category__and’ => array(9,13),’orderby’=>title,’order’=>ASC));

    djsmacedo – I could not get your solution, which is the same as the one in the trac, to work. I have multiple loops in the template file I am using it with – could that be a problem?

    If I remove the tags=tag1+tag1 portion of the query_posts, it does work.

    This may should be posted in another topic, but I thought I’d ask here since it seems like pretty close to the same thing.

    I’m using a function to pull attachments from a selected category using the code:
    $posts=query_posts('showposts=1&cat='.$cat);

    But, when I call the function and have the cat that I wish to be used, it’s almost like it’s being ignored because it’ll pull the attachments from any category that I have.

    My question is, is there another way to write that which would work? Or, why is it not limiting the post to the category?

    Thank you!

    As moshu pointed out earlier, check the documentation on query_posts and the loop. You should only be using query_posts to modify the posts used in the main loop, nowhere else. Anywhere else, you need to create a new WP_Query object. Fortunately, this is very easy.

    $my_query = WP_Query();

    Then you can use $my_query->query_posts() to do whatever it is that you’re trying to do with query_posts(). And get predictable results.

    Thanks for the reply taffeljohan!

    I’m trying to combine that w/ what I already have but I keep throwing an error.

    <?php function randomimage($size=medium,$num=1,$cat=0) {
    	$posts=query_posts('showposts=1&cat='.$cat);
    	foreach( $posts as $post ) {

    Looks like I have some more reading to do. Thanks again!

    Right. WP_Query doesn’t have a query_posts method. I meant $my_query->query();

    So something like:

    <?php function randomimage($size=medium,$num=1,$cat=0) {
        $my_query=new WP_Query();
        $posts=$my_query->query('showposts=1&cat='.$cat);
        foreach( $posts as $post ) {

    should do it…

    That works, but it’s still not limiting the category. I don’t understand why it’s ignoring the category that I set. Thank you very much for your time though!! 🙂

    Weird. I assume that you’re using the integer id for the category and not the name (otherwise you need to use category_name instead of cat).

    What are you doing to get the attachments?

    I’m using some code I found in the forum here and just calling the function where I’m displaying the images.

    <?php function randomimage($size=medium,$num=1,$cat=0) {
        $my_query=new WP_Query();
        $posts=$my_query->query('showposts=1&cat='.$cat);
        foreach( $posts as $post ) {
    		if ( $images = get_children(array(
    			'post_type' => 'attachment',
    			'numberposts' => $num,
    			'orderby' => 'rand()',
    			'post_mime_type' => 'image',)))
    		{
    			$zeiger = 1;
    
    			foreach( $images as $image ) {
    				if ($zeiger != $num) {
    					$zwischen="&nbsp;&nbsp;&nbsp;&nbsp;";
    				} else {
    					$zwischen = "";
    					$zeiger = 1;
    				}
    
    $attachmenturl=get_permalink($image->ID);
    $attachmentimage=wp_get_attachment_image( $image->ID, $size );
    				echo '<a href="'.$attachmenturl.'">'.$attachmentimage.'</a>'.$zwischen;
    				$zeiger++;
    			}
    		} else {
    			echo "No Image";
    		}
    	}
    }
    ?>

    Thanks!!

    I think you need to set the ‘post_parent’ in the get_children() call. Since you’re not in the loop, it has no way of getting at the $post variable and uses the default 0, which means all images are retrieved.

    Try adding 'post_parent'=>$post->ID to the array you pass to get_children.

    Thanks! So close, now it is limited to the category, but only pulls the images from my last post in the selected category. Thank you again!

    That’s because you have ‘showposts=1’ in your query. I thought that was on purpose.

Viewing 15 replies - 16 through 30 (of 34 total)
  • The topic ‘query_posts in category with tag’ is closed to new replies.