• JRGould

    (@jrgould)


    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 - 1 through 15 (of 34 total)
  • 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.

    Thread Starter JRGould

    (@jrgould)

    *bump*

    Anybody know of anything?

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

    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 =/

    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.

    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.

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

    Bummer!

    Thread Starter JRGould

    (@jrgould)

    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…

    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?

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

    I checked the usage section and followed it but it’s not working for me. Please see my category page in https://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>

    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)

    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
    }
    ?>

    @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

    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

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

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