WordPress.org

Ready to get started?Download WordPress

Forums

query_posts in category with tag (35 posts)

  1. JRGould
    Member
    Posted 6 years ago #

    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?

  2. blowski
    Member
    Posted 6 years ago #

    I have the same problem. On my category page, I want to have a section for 'featured' articles, so I have tagged articles with this.

    This is the line of code:
    query_posts($query_string . '&tag=featured');

    but like JRGould, it doesn't return any posts. It returns posts if I use either the category or the tag, but not if I do both together - on any category.

  3. JRGould
    Member
    Posted 6 years ago #

    *bump*

    Anybody know of anything?

    it really seems like I should just be able to use
    query_posts("cat=6&tag=featured&showposts=5");

  4. FernandoNery
    Member
    Posted 6 years ago #

    i have the same exactly problem as you guys...
    but instead, im trying to desappear with two categories:

    query_posts("cat=-57,-81&posts_per_page=20");

    but it just don't return any result for me...
    i was thinkings that it could be some MySQL error, since this two categories results in more than 1000 posts, but if i make the query in another page it runs ok =/

  5. SimonJ
    Member
    Posted 6 years ago #

    Same thing here!

    I'm unable to use "tag" AND "cat"... If I use only one of them "tag" OR "category" all is OK, but together, I get no post...

    Like here, for instance :

    query_posts('cat=12&tag=vol02no15');

    From what what I can tell, in WP 2.3.+, the category AND the tags are in the same wp_terms table in the database. Maybe it's a part of the problem... ?

    Any WP Guru have a clue how to solve this problem... ?

    S.

  6. SimonJ
    Member
    Posted 6 years ago #

    Hola again!

    Still no advice on ths issue ?

    Can anyone could confirm if we can, or can't use "cat" and "tag" together in the same query_posts statement ?

    If we normally can, then there is a bug... :-)

    If the fact that we can't is normal, then forget it, ther is no issue and I will stop scratching my head... :-)

    Thx++

    S.

  7. aradams
    Member
    Posted 6 years ago #

    Same problema here. I can use cat= OR tag= in query_posts but not both.

    Bummer!

  8. JRGould
    Member
    Posted 6 years ago #

    okay, so I kind of just ditched my original task that I needed this for, but managed to come around to it again. Here's what I devised:

    You cannot use query_posts for this, I don't know why it has not been included, but it hasn't so I'm over it. What you need to do is put together a custom query somewhat like this:

    SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->posts.post_status = 'publish'
    AND $wpdb->posts.post_type = 'post'
    AND $wpdb->term_taxonomy.term_id IN ([category term_id],[tag term_id])
    AND !EXISTS(
    SELECT NULL
    FROM $wpdb->term_relationships
    LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->term_relationships.object_id = $wpdb->posts.ID
    AND $wpdb->term_taxonomy.term_id IN ([excluded tag or cat term_ids])
    )

    What you're doing here is querying for 'terms' whether they are categories or post tags, based on their id, so you'll have to go into your DB and find out the ids of tags. I'm sure you could do another join of term_taxonomy and terms to be able to query based on their name but I didn't really feel like it, plus this query is already obfuscated enough. The hard part here is excluding terms, or querying for the intersect of terms. The way I'm doing it is by running a subquery (AND EXISTS (foo) for intersects and AND !EXISTS bar) for exclusions).

    Sorry for this being kind of convoluted, it's just something I figured out in an unrelated context so I thought I'd post it here for reference. Reply here if you want to pick my brain on the topic, or if I'm an idiot and have gone about this all wrong...

  9. sepamy
    Member
    Posted 5 years ago #

    I'd like one of my cat breeds categories to show only one post a page. Rest should follow the option set in admin.

    So for this one category I'm using a new category-17.php file. with query_posts('showposts=1')

    But my navigation breaks down. 'Older Entries' link does not shows a Not found error. I do have more posts in the same category though.

    Any suggestions?

  10. moshu
    Member
    Posted 5 years ago #

    Read the documentation: Template_Tags/query_posts
    Special attention to the Usage section referring to preserving parameters like page...

  11. mizrach
    Member
    Posted 5 years ago #

    I checked the usage section and followed it but it's not working for me. Please see my category page in http://www.devstandards.org/category/application/java
    Select Best Practices tab. When I click "Older Entries" it says "Not Found".

    Please see my code below. I hope you can help me the problem.

    <?php
    
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$offset = ($paged * 1);
       echo "page:" . $page;
       echo "offset:" . $offset . "<br />";
       $this_category = get_category($cat);
       query_posts("&cat=8&showposts=1&offset=$offset&tag=$this_category->cat_name+$this_category->cat_name");
    
      // the Loop
       if (have_posts()) : while (have_posts()) : the_post(); ?>
          <a href="<?php the_permalink() ?>" rel="bookmark"><b><?php the_title(); ?></b></a>
          <?php  the_content('Read the full post »'); ?>
        <hr>
       <?php endwhile; else: ?>
    			<p><?php _e('Sorry, there is no best practices yet in this category.'); ?></p>
       <?php endif;  ?>
    			<p><?php  next_posts_link('&laquo; Older Entries'); ?></p>
  12. moshu
    Member
    Posted 5 years ago #

    No, you didn't read it. Not carefully. It says to use
    query_posts($query_string .
    in order to to preserve the information it receives via the URL (such as page number or category)

  13. mizrach
    Member
    Posted 5 years ago #

    I changed the code to the following to use the query_posts($query_string. However, the $query_string is storing the "Java" category(. But in the "Best Practices" tab, all the post is under the "Best Practices" category with tag of "Java" and not the "Java" category. I want to navigate to the next page showing the next set of post under the "Best Practices" category with tag of "Java".

    I appreciate any help on this.

    <?php
    if ($cat != 9  && $cat != 10 )
    {
    ?>
    <?php
    
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$offset = ($paged * 1);
       echo "page:" . $page;
       echo "offset:" . $offset . "<br />";
       echo "querystring:" . $query_string . "<br />";
       $this_category = get_category($cat);
       echo "querystring1:" . $ds_qry_string . "<br />";
    
       $ds_qry_string = "&cat=8&showposts=1&offset=$offset&tag=$this_category->cat_name+$this_category->cat_name";
       echo "querystring1:" . $ds_qry_string . "<br />";
       query_posts("&cat=8&showposts=1&offset=$offset&tag=$this_category->cat_name+$this_category->cat_name");
    
      // the Loop
       if (have_posts()) : while (have_posts()) : the_post(); ?>
          <a href="<?php the_permalink() ?>" rel="bookmark"><b><?php the_title(); ?></b></a>
          <?php  the_content('Read the full post »'); ?>
        <hr>
       <?php endwhile; else: ?>
    			<p><?php _e('Sorry, there is no best practices yet in this category.'); ?></p>
       <?php endif;  ?>
    			<p><?php  next_posts_link('&laquo; Older Entries'); ?></p>
    <?php
    }
    ?>
  14. sudesh_poojari
    Member
    Posted 5 years ago #

    @JRGould use category_name instead of cat might work;

    I have different problem, did any one here have two query_posts in same page, i found that the second query_posts seems to preserve the previous query_post value, and only changes the values we used.

    Can anybody confirm, and if so then how to reset it.

    Thanks,

    Sudesh

  15. sudesh_poojari
    Member
    Posted 5 years ago #

    My previous post doesn't seem clear

    by values i mean

    your first query had query_post("cat=1&posts_per_page=10");

    and second query_post("posts_per_page=8");

    It would change the posts_per_page for second query but not category, if i m right please somebody confirm;

    sudesh

  16. moshu
    Member
    Posted 5 years ago #

    Read the documentation about The_Loop - special attention to using multiple loops.

  17. Yoris
    Member
    Posted 5 years ago #

    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');

  18. djsmacedo
    Member
    Posted 5 years ago #

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

  19. Yoris
    Member
    Posted 5 years ago #

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

  20. aidee
    Member
    Posted 5 years ago #

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

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

  21. mfshearer72
    Member
    Posted 5 years ago #

    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.

  22. shaneholden
    Member
    Posted 5 years ago #

    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!

  23. taffeljohan
    Member
    Posted 5 years ago #

    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.

  24. shaneholden
    Member
    Posted 5 years ago #

    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!

  25. taffeljohan
    Member
    Posted 5 years ago #

    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...

  26. shaneholden
    Member
    Posted 5 years ago #

    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!! :)

  27. taffeljohan
    Member
    Posted 5 years ago #

    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?

  28. shaneholden
    Member
    Posted 5 years ago #

    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!!

  29. taffeljohan
    Member
    Posted 5 years ago #

    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.

  30. shaneholden
    Member
    Posted 5 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic