Forums

[resolved] How to Include One Tag While Excluding Another (16 posts)

  1. michaelhyatt
    Member
    Posted 2 years ago #

    I want to include in my sidebar posts that include a specific tag and exclude another one.

    Quick background: I collect quotes on my site. They are all tagged “Quote.” However, some are too long for display in the sidebar. For these, I have included a “Long” tag, in addition to the “Quote” tag.

    So the logic is that I want to include in the loop all posts that are tagged “Quote” but not tagged “Long.”

    I tried the following code but it does not work. It does include the post that are tagged “Quote,” but it does not exclude posts that are tagged “Long”:

    <?php
    	query_posts('showposts=1&tag=Quote,-Long&orderby=rand');
    	if (have_posts()) : while (have_posts()) : the_post(); ?>
            <?php echo the_content(); ?>
    	<?php endwhile; endif; ?>  
    
     <?php wp_reset_query(); ?>

    What am I missing here?

  2. jtohline
    Member
    Posted 2 years ago #

    You shouldn't have to even mention "long" to exclude it. Your query will show only those items you want to show.

    <?php
    	query_posts('showposts=1&tag=Quote&orderby=rand');
    	if (have_posts()) : while (have_posts()) : the_post(); ?>
            <?php echo the_content(); ?>
    	<?php endwhile; endif; ?>  
    
     <?php wp_reset_query(); ?>
  3. human3rror
    Member
    Posted 2 years ago #

    That's one option... but the problem is I think Mike has all of his quotes with the "quote" tag but some are too "long" (the reason he has the "long" tag as well.

    Probably a better solution is multiple if-then. I'll look at some other options as well.

  4. michaelhyatt
    Member
    Posted 2 years ago #

    I tried this previously and it did not work. The problem is that these posts have two tags: Quote and Long. In another place on my blog, I collect all the Quotes whether they are long or short. So the Long tag is an additional tag.

    Thanks for your help.

  5. human3rror
    Member
    Posted 2 years ago #

    wow. mike is fast...

  6. michaelhyatt
    Member
    Posted 2 years ago #

    I just wish I wasn't such a hack!

  7. human3rror
    Member
    Posted 2 years ago #

    Try adding something like:

    if( has_tag('Long') {}
    else{}
  8. michaelhyatt
    Member
    Posted 2 years ago #

    Not quite sure how I integrate that, but the logic makes sense.

  9. idowens
    Member
    Posted 2 years ago #

    This function from the Woo founder may help: How to exclude specific tag from query_posts

  10. michaelhyatt
    Member
    Posted 2 years ago #

    I looked at that but, unfortunately, am not smart enough to actually translate it into the exact code snippet I need. If anyone can do that, I am in your debt!

  11. idowens
    Member
    Posted 2 years ago #

    How many quotes are you working with? If it's a smaller number, it may be easier to add a custom field & key to the shorter quotes, and then filter the loop based on that field. If this sounds like a possibility, I'll put together a code.

  12. michaelhyatt
    Member
    Posted 2 years ago #

    I probably have 50 quotes, but it is growing every week. Thanks for your offer!

  13. idowens
    Member
    Posted 2 years ago #

    I'd still use a custom field, written to the database, so that you can manipulate the data in the future.

    See this thread: http://wordpress.org/support/topic/353347

    You can whip up a plugin in 15 min or so that would put a fancy little box on your posts page that would give you a few checkboxes: (1) quote (2) short (3) long, etc. Then, you could query posts based on the value of the meta key.

  14. andrewbuckman
    Member
    Posted 2 years ago #

    This should do what you're looking for (adapted from the link idowens mentioned by the Woo founder).

    <?php
    	$tag = $wpdb->get_var($wpdb->prepare("SELECT term_ID FROM $wpdb->terms WHERE name='Long'"));
    	$args = array(
    		'showposts' => 1,
    		'tag' => 'Quote',
    		'tag__not_in' => array($tag),
    		'orderby' => 'rand'
    	);
    	query_posts($args);
    	if (have_posts()) : while (have_posts()) : the_post();
    		echo the_content();
    	endwhile; endif;
    
    	wp_reset_query();
    ?>
  15. michaelhyatt
    Member
    Posted 2 years ago #

    Andrew, you win the prize. That works! Awesome. Thank you so much.

    (I would have never come up with that on my own.)

  16. andrewbuckman
    Member
    Posted 2 years ago #

    No problem, glad it worked!

Topic Closed

This topic has been closed to new replies.

About this Topic