• Resolved michaelhyatt

    (@michaelhyatt)


    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?

Viewing 15 replies - 1 through 15 (of 15 total)
  • 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(); ?>
    Anonymous User 757909

    (@anonymized-757909)

    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.

    Thread Starter michaelhyatt

    (@michaelhyatt)

    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.

    Anonymous User 757909

    (@anonymized-757909)

    wow. mike is fast…

    Thread Starter michaelhyatt

    (@michaelhyatt)

    I just wish I wasn’t such a hack!

    Anonymous User 757909

    (@anonymized-757909)

    Try adding something like:

    if( has_tag('Long') {}
    else{}
    Thread Starter michaelhyatt

    (@michaelhyatt)

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

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

    Thread Starter michaelhyatt

    (@michaelhyatt)

    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!

    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.

    Thread Starter michaelhyatt

    (@michaelhyatt)

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

    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.

    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();
    ?>
    Thread Starter michaelhyatt

    (@michaelhyatt)

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

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

    No problem, glad it worked!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘How to Include One Tag While Excluding Another’ is closed to new replies.