Support » Fixing WordPress » Query Question – Adding Not Equal to Logic

  • Resolved kraftomatic

    (@kraftomatic)


    Hey All,

    I have the following piece of PHP code:

    <?php $carousel = new WP_query(); $carousel->query('showposts='.$mytheme['carouseln'].'&cat=2&tag=doug&orderby=asc'); ?>

    I’d like to make the tag piece look as “tag!=doug”, but that isn’t working. How can I specify some parameters I want to *not* include here?

    Thanks in advance.

Viewing 8 replies - 1 through 8 (of 8 total)
  • esmi

    (@esmi)

    Forum Moderator

    I thought that would, but it doesn’t work unfortunately.

    That’s because you need to use the tag ID and not the name.

    tag__in
    and
    tag__not_in

    both take an array of tag IDs to exclude/include ..(depending on which you use)

    t31os_ – I do see that in regard to the array. However, what does using the tag ID get me here?

    In the code I have in the first post, using “tag=doug” works for displaying any posts tagged as doug. What I’m trying to do is display posts that do not have that tag. So I simply need a logic operator that will work like “!=”, which doesn’t seem to be working in the code I’m using.

    Perhaps I need to do some work on the query before that line of code and just grab posts without certain tags on them?

    You need to use tag id numbers when using tag__not_in.

    <?php
       $args=array(
       'showposts'=>-1,
       'caller_get_posts'=>1,
       'category__in' => array(3),
       'tag__not_in' => array(37,47),
       );
    $my_query = new WP_Query($args);
    global $wp_query;
    $wp_query->in_the_loop = true;  //need this to cause the_tags to work
    ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php the_tags(); ?>
    <?php endwhile;
    wp_reset_query();  // Restore global post data stomped by the_post().
    
    ?>

    oops see t31os_ got there already

    Not all the query_posts args support the comparison operator (only meta parameters do if i remember correctly), so you have to look at the ones that exist for exclusions, in this case tag__not_in .. which excepts the tag ID..

    If the tag doug has an ID of 5, then you would use something like..

    <?php
    $args = array(
    	'showposts' => $mytheme['carouseln'],
    	'cat' => 2,
    	'tag__not_in' => array(5), // Assuming 5 is the ID for the tag doug
    	//'tag' => 'doug',
    	'order' => 'asc'
    );
    // NOTE: Orderby sets the database field to sort the results by - Order sets the direction, ASC or DESC
    $carousel = new WP_query();
    $carousel->query( $args );
    ?>

    It’s the same deal as what you’re doing with the category, using the ID instead of the name..

    Further reading: http://codex.wordpress.org/Template_Tags/query_posts#Tag_Parameters

    Perfect! Thanks guys.

    Matt Hill

    (@gambit37)

    Sorry, I removed my comment, I didn’t see this topic was marked as resolved.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Query Question – Adding Not Equal to Logic’ is closed to new replies.