I'm using this piece of code to generate Related Posts by looking at the tags of posts. The problem is I have use a tag named Featured in order for a post to show up in a slideshow. So posts are showing up as related because of that matching Featured tag, even though they are not alike at all.

So is there an easy to to exclude the tag "Featured" in this code?

<?php $tags = get_the_tags(); ?>
<?php if($tags): ?>
<?php $related = get_related_posts($post->ID, $tags); ?>
<?php if($related->have_posts() && $related->found_posts >= 3 && get_option('pyre_related') == 'On'): ?>

I am guessing there needs to be a tag__not_in or something similar included but I just don't know PHP well enough to know the syntax.

UPDATED: I didn't end up using Chip Bennett's answer (which may have worked). I had to find the functions.php which was hidden in a special subfolder inside the theme. Using tag__not_in did not seem to work. It seem to always return the same posts (the most recent posts even if they shared no similar tags. Here is the entire get_related_posts function.

function get_related_posts($post_id, $tags = array()) {
    $query = new WP_Query();

    $post_types = get_post_types();
    unset($post_types['page'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);

    if($tags) {
        foreach($tags as $tag) {
            $tagsA[] = $tag->term_id;

    $args = wp_parse_args($args, array(
        'showposts' => 4,
        'post_type' => $post_types,
        'post__not_in' => array($post_id),
        'tag__in' => $tagsA,
        'tag__not_in' => 795,
        'ignore_sticky_posts' => 1,

    $query = new WP_Query($args);

    return $query;
  • What does the get_relateds_posts() function look like? Is it one you've written? – sanchothefat Mar 15 '12 at 15:57
  • That is a good question. This is from a theme I purchased. Pretty sure the author wrote it, it is not using a plugin. My block of code is from the single.php and that block of code is the only one in that page that references get_related_posts. Where else should I look for the function? – Dustin J Mar 15 '12 at 16:30
  • It'll be in functions.php or an include file - I'd avoid editing it if it's written by someone else so the approach Chip suggested is probably the direction to go. Is a tag the only way to target a post to the featured bit? – sanchothefat Mar 15 '12 at 17:04
  • Found it. I search the normal functions.php but this function was actually a different functions.php in a special subfolder inside the theme. I was able to edit that to include a tag__not_in. Thanks for getting me on the right track though! – Dustin J Mar 15 '12 at 18:09
  • Glad you found a working solution! :) Please post it as an answer, and then accept that answer, to help close out the question. – Chip Bennett Mar 15 '12 at 18:17

The get_the_tags() function returns an array of objects. So, you can step through the this array, and unset the object for the "Featured" tag, perhaps like so:

$tags = get_the_tags();

foreach ( $tags as $tag_key => $tag_object ) {
    if ( 'featured' == $tag_object->slug ) {
        unset( $tags[$tag_key] );
// Continue with the rest of the code, here

Note: untested. Also, assumes that the slug for the "Featured" tag is featured.

  • I thought this would be the ticket but it looks like nothing has changed. Didn't break anything but didn't seem to work. I edited my original post with what I put in using your code. – Dustin J Mar 15 '12 at 16:23
  • OOPS! Mistake in my unset() call. It needs to reference the $tags array, not just the object within the array. See updated answer. – Chip Bennett Mar 15 '12 at 18:05
  • I tried your updated code and got this error message. Illegal offset type in unset in /_path to folder_/single.php on line 220 – Dustin J Mar 15 '12 at 18:23
  • Can you do a var_dump( $tags ); immediately after $tags = get_the_tags();? I'm curious what the array looks like, and why this method would result in an illegal offset type error. – Chip Bennett Mar 15 '12 at 18:37
  • 1
    I'm guessing it's not working because $tag_object references the value and not the key, so won't work for unset(). Change the foreach to foreach( $tags as $tag_key => $tag_object ) and unset to unset( $tags[$tag_key] ) – Boone Gorges Mar 15 '12 at 18:44
            $tags = get_the_tags(); 
            foreach( $tags as $tag_key => $tag_object ) {
                if ( 'featured' == $tag_object->slug ) {
                    unset( $tags[$tag_key] );
            $related = get_related_posts($post->ID, $tags);
            if($related->have_posts() && $related->found_posts >= 3 && get_option('pyre_related') == 'On'): 

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.