3

While following this article to get tags by date, I was wondering if I could exclude certain tags from what gets returned. The point is to have trending tags but some tags will always be there and I don't want to include those ones.

Here is the code:

    <?php
    $how_many_posts = 50;
    $args = array(
        'posts_per_page' => $how_many_posts,
        'orderby' => 'date',
        'order' => 'DESC',
    );
    // get the last $how_many_posts, which we will loop over
    // and gather the tags of
    query_posts($args);
    //
    $temp_ids = array();
    while (have_posts()) : the_post(); 
        // get tags for each post
        $posttags = get_the_tags();
        if ($posttags) {
            foreach($posttags as $tag) {
                // store each tag id value
                $temp_ids[] = $tag->term_id;
            }
        }
    endwhile;
    // we're done with that loop, so we need to reset the query now
    wp_reset_query();
    $id_string = implode(',', array_unique($temp_ids));
    // These are the params I use, you'll want to adjust the args
    // to suit the look you want    
    $args = array(
        'smallest'  => 15, 
        'largest'   => 15,
        'unit'      => 'px', 
        'number'    => 10,  
        'format'    => 'flat',
        'separator' => "\n&bull;\n",
        'orderby'   => 'count', 
        'order'     => 'DESC',
        'include'   => $id_string,  // only include stored ids
        'link'      => 'view', 
        'echo'      => true,

    );
    wp_tag_cloud( $args );
    ?>

UPDATE: I am wondering since doing passing an 'exclude' object in the args for wp_tag_cloud didn't work as one answer suggested, if doing an unset before the wp_tag_cloud runs is how to make this work? The problem is is that I don't know much about php or Wordpress so I'm not sure how to work that in to the above code.

                foreach( $tags as $tag_key => $tag_object ) {
                if ( 'tag1' == $tag_object->slug || 'tag2' == $tag_object->slug ) {
                        unset( $tags[$tag_key] );
                        }
                    }

Thanks for your help!

UPDATE 2: @artlung answer worked perfectly.

2 Answers 2

2
<?php
    $how_many_posts = 50;
    $exclude_these_term_ids = array(
       10,
       20,
       35,
    );
    $args = array(
        'posts_per_page' => $how_many_posts,
        'orderby' => 'date',
        'order' => 'DESC',
    );
    // get the last $how_many_posts, which we will loop over
    // and gather the tags of
    query_posts($args);
    //
    $temp_ids = array();
    while (have_posts()) : the_post(); 
        // get tags for each post
        $posttags = get_the_tags();
        if ($posttags) {
            foreach($posttags as $tag) {
                // store each tag id value
                // that is not in the $exclude_these_term_ids
                // array
                if (!in_array($tag->term_id, $exclude_these_term_ids)) {
                    $temp_ids[] = $tag->term_id;
                }
            }
        }
    endwhile;
    // we're done with that loop, so we need to reset the query now
    wp_reset_query();
    $id_string = implode(',', array_unique($temp_ids));
    // These are the params I use, you'll want to adjust the args
    // to suit the look you want    
    $args = array(
        'smallest'  => 15, 
        'largest'   => 15,
        'unit'      => 'px', 
        'number'    => 10,  
        'format'    => 'flat',
        'separator' => "\n&bull;\n",
        'orderby'   => 'count', 
        'order'     => 'DESC',
        'include'   => $id_string,  // only include stored ids
        'link'      => 'view', 
        'echo'      => true,

    );
    wp_tag_cloud( $args );
    ?>
0
1

The function get_the_tags returns an array of objects, one object for each tag assigned to the post. If you want to exclude some tags you can do it in the wp_tag_cloud function by the tag id or slug. Example:

$args = array(
        'smallest'  => 15, 
        'largest'   => 15,
        'unit'      => 'px', 
        'number'    => 10,  
        'format'    => 'flat',
        'separator' => "\n&bull;\n",
        'orderby'   => 'count', 
        'order'     => 'DESC',
        'include'   => $id_string,  // only include stored ids
        'exclude'   => 'tag1, tag2, tag3',
        'link'      => 'view', 
        'echo'      => true,

    );
    wp_tag_cloud( $args );
5
  • I tried using the exclude object as you suggested but the tags I tried to exclude still showed up on the page.
    – Dustin J
    Commented Jul 19, 2012 at 20:26
  • can you try by the tag id and start with only one. Commented Jul 19, 2012 at 20:33
  • Yeah, I tried that. I used the id of 76 with and without the single quote and before and after the include object in the args. None of those tests were able to actually exclude that tag though. It still gets returned.
    – Dustin J
    Commented Jul 19, 2012 at 20:35
  • I updated my first post to include an unset suggestion. I do not know a whole lot about php, but maybe the unset will remove it before the tags before they get set in the $id_string? Can you help?
    – Dustin J
    Commented Jul 20, 2012 at 13:19
  • The Codex explains that if the "If 'include' is non-empty, 'exclude' is ignored." So that is why it must not be working. codex.wordpress.org/Function_Reference/get_tags
    – Dustin J
    Commented Jul 20, 2012 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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