-1

How can i exclude the display of a specific tag or tags on the results of related posts?

this is the code http://pastebin.com/Ksh7NY7g

/*-----------------------------------------------------------------------------------*/
/* Related Posts Function
/*-----------------------------------------------------------------------------------*/

function ag_get_related_post_args($id){

    global $relatedcatargs, $relatedargs;

    $rel_tagnames = '';
    $rel_catnames = '';

    if ( !($relatednumber = of_get_option('of_related_number') ) ) { $relatednumber = '2'; } else { $relatednumber = of_get_option('of_related_number'); } 

    // Get related post tag names
    $rel_tags = get_the_tags($id);

    if ($rel_tags) {
        // Get list of tag names and set arguments for loop
    foreach($rel_tags as $rel_tag) {
          $rel_tagnames .= $rel_tag->slug . ',';
        }
        $relatedargs = array(
          'ignore_sticky_posts' => 1,
          'tag' => $rel_tagnames,
          'post__not_in' => array($id),
          'showposts' => $relatednumber,
          'orderby' => 'rand'
        );
    }

    // Get list of category names and set arguments for loop
    $post_cats = wp_get_post_categories($id);

    foreach($post_cats as $post_cat) {
      $rel_catnames .= get_cat_name( $post_cat ) .',';
    }

    $relatedcatargs = array(
      'ignore_sticky_posts' => 1,
      'category__in' => $post_cats,
      'post__not_in' => array($id),
      'showposts' => $relatednumber,
      'orderby' => 'rand'
    );

}

function load_custom_scripts() {
    global $wp_version;

    if (version_compare($wp_version, '3.4.2', '>')) {
        wp_register_script('my-jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js', 'jquery');
        } else {
        wp_register_script('my-jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js', 'jquery');
    } 


    wp_enqueue_script( 'my-jquery-ui' );
}

add_action('admin_init', 'load_custom_scripts'); 
global $relatedargs, $relatedcatargs;
// Query Posts by tag
$my_query = new WP_Query($relatedargs);

//If there are posts
if( $my_query->have_posts()) { ?>

<!-- Related Posts -->
<div class="relatedposts">
  <h3><?php _e('Related Posts', 'framework'); ?></h3>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

  <a href="<?php the_permalink();?>" class="one_col half">
    <div class="featuredinner">
          <h2><?php 
              if (strlen($post->post_title) > 40) {
                  echo substr(the_title($before = '', $after = '', FALSE), 0, 80) . '...'; 
              } else {
                  the_title();
              }  ?>                      
              <span class="date"><?php the_time(get_option('date_format')); ?> | <?php echo get_the_author(); ?></span></h2>
              <div class="featuredoverlay"></div>
              <?php the_post_thumbnail('blogonecol', array('class' => 'scale-with-grid')); /* post thumbnail settings configured in functions.php */ ?>
    </div>
  </a>

    <?php endwhile;  ?>

  <div class="clear"></div>
</div>
<?php } wp_reset_query(); // end if there are posts and reset the query ?>
  • Please edit your code into the question. – s_ha_dum Jun 21 '13 at 12:43
  • i tried but see right now who it shows on post with <pre></pre> – Alex Jun 21 '13 at 12:52
  • That won't happen if you use the code formatting tools provided by the editor. Look for the {} button. – s_ha_dum Jun 21 '13 at 12:54
2

The Codex has almost exactly what you are asking:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'movie_genre',
            'field' => 'slug',
            'terms' => array( 'action', 'comedy' )
        ),
        array(
            'taxonomy' => 'actor',
            'field' => 'id',
            'terms' => array( 103, 115, 206 ),
            'operator' => 'NOT IN'
        )
    )
);
$query = new WP_Query( $args );

You just need to remove the couple of parts you don't need, and of course change the post_type and taxonomy details to fit your data.

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'post_tag',
            'field' => 'id',
            'terms' => array( 103, 115, 206 ), // change these
            'operator' => 'NOT IN'
        )
    )
);
$query = new WP_Query( $args );

Best I can do with the minimal detail provided in the question.

Based on new information in an edit to the question:

The 'tag' => $rel_tagnames, pattern you are using is deprecated. You should be using a tax_query as above. Since you are already including a list of tags to search, you shouldn't need to exclude any. When you include specific tags, others are de facto excluded. So what you would want is this:

if ($rel_tags) {
    $relatedargs = array(
        'ignore_sticky_posts' => 1,
        'post__not_in' => array($id),
        'showposts' => $relatednumber,
        'orderby' => 'rand'
    );
    // Get list of tag names and set arguments for loop
    foreach($rel_tags as $rel_tag) {
      // exclude some tags
      $excluded_tags = array (1,2,3);
      if (in_array($rel_tag->term_id,$excluded_tags)) continue;
      $rel_tagnames[] = $rel_tag->term_id;
    }
    $relatedargs['tax_query'] = array(
      array(
        'taxonomy' => 'post_tag',
        'field' => 'id',
        'terms' => $rel_tagnames,
      )
    )
}

// the rest of your code

I changed that to use IDs instead of slugs. It is probably more efficient to search those than the slugs, though I haven't benchmarked it, and converted to a tax_query. Tags are excluded when you build the "include" list, instead of trying to do that in the query itself.

Why does your function set globals instead of just returning data?

  • and where exactly on my code i add this? – Alex Jun 21 '13 at 13:01
  • You need to work the tax_query part into your $relatedargs arguments for your query. As you didn't post what $relatedargs already looks like I couldn't really do that for you. – s_ha_dum Jun 21 '13 at 13:04
  • i update my post check – Alex Jun 21 '13 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.