WordPress.org

Ready to get started?Download WordPress

Forums

Exclude posts containing a particular tag (4 posts)

  1. stiwdio
    Member
    Posted 4 years ago #

    Hi
    I am fairly new to WP and PHP so please forgive my lack of knowledge; Bascailly I've been setting up template pages to show posts which have been tagged with certain tags;

    It looks like I've got working; however what I need to do now is the opposite; to exclude posts containing certain tags; I have paasted the current code which includes certain tagged posts here, can anyone suggest how to modify it to exclude them;
    Thanks for your help
    Darren

    <?php
    /*
    Template Name: Helens Posts
    */
    get_header(); ?>
    
    <div id="content_box">
    	<div id="content" class="posts">
    	<?php
    $term = get_term_by('slug','helen', 'post_tag');
    $args=array(
      'tag__in' => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <h4><?php the_time('F jS, Y') ?><!-- by <?php the_author() ?> --> &middot; <a>#comments"><?php comments_number('No Comments', '1 Comment', '% Comments'); ?></a> &middot; <?php if (!is_attachment()) { the_category(', '); } ?> <?php edit_post_link('Edit', ' &middot;', ''); ?></h4>
    <div class="entry">
    					<?php the_content('<p>Read the rest of this entry &raquo;</p>'); ?>
    					<?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
    				</div><?php if (!is_attachment()) { ?><p class="tagged"><strong>Tags:</strong> <?php the_tags('','&middot;',''); ?></p><?php } ?>
    <div class="clear"></div>
    <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>	
    
     </div>
    <?php get_sidebar(); ?>
    </div>
    
    <?php get_footer(); ?>
  2. Michael Fields
    Themer
    Posted 4 years ago #

    You only have to change the array key from tag__in to tag__not_in.

    $term = get_term_by( 'slug', 'exclude', 'post_tag' );
    $args = array(
    	'tag__not_in' => array( $term->term_id ),
    	'post_type' => 'post',
    	'post_status' => 'publish',
    	'posts_per_page' => -1,
    	'caller_get_posts'=> 1
    );
  3. justbishop
    Member
    Posted 3 years ago #

    Hey, anyone know how I'd modify this to exclude 2 different tags?

  4. justbishop
    Member
    Posted 3 years ago #

    Nevermind, I figured it out :D

    $term = get_term_by('slug','blog', 'post_tag');
    $term2 = get_term_by('slug','gallery', 'post_tag');
    $args=array(
      'tag__not_in' => array($term->term_id, $term2->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'numberposts'=>20,
      'caller_get_posts'=> 1
    );

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.