0

I have the following code and I'm trying to check to see if a specific tag exists, and if so, I need to echo it once. The problem is when the tag exists in multiple places, it echos multiple times.

// LOOP ARGUMENTS
$args = array(
    'post_type' => 'xyz_members', 
    'posts_per_page' => -1, 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'tax_query' => array(
        array(
            'taxonomy' => 'member_types',
            'field' => 'slug',
            'terms' => 'current-class',
            'operator' => 'NOT IN'
        )
    )
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();                              
    // CUSTOM CONTENT
    $postTags = get_the_tags();
    if ($postTags) while($postTags as $tag) {
        if ($tag->slug == '2006') { ?><li id="_2013" class="filter on">2006</li><?php }
        if ($tag->slug == '2007') { ?><li id="_2013" class="filter on">2007</li><?php }
        if ($tag->slug == '2008') { ?><li id="_2013" class="filter on">2008</li><?php }
        if ($tag->slug == '2009') { ?><li id="_2013" class="filter on">2009</li><?php }
        if ($tag->slug == '2010') { ?><li id="_2013" class="filter on">2010</li><?php }
        if ($tag->slug == '2011') { ?><li id="_2013" class="filter on">2011</li><?php }
        if ($tag->slug == '2012') { ?><li id="_2013" class="filter on">2012</li><?php }
        if ($tag->slug == '2013') { ?><li id="_2013" class="filter on">2013</li><?php }
        if ($tag->slug == '2014') { ?><li id="_2013" class="filter on">2014</li><?php }
        if ($tag->slug == '2015') { ?><li id="_2013" class="filter on">2015</li><?php }
        if ($tag->slug == '2016') { ?><li id="_2013" class="filter on">2016</li><?php }
        if ($tag->slug == '2017') { ?><li id="_2013" class="filter on">2017</li><?php }
        if ($tag->slug == '2018') { ?><li id="_2013" class="filter on">2018</li><?php }
        if ($tag->slug == '2019') { ?><li id="_2013" class="filter on">2019</li><?php }
        if ($tag->slug == '2020') { ?><li id="_2013" class="filter on">2020</li><?php }
    }
/* END WHILE AND RESET QUERY */ 
endwhile; wp_reset_query();
2
  • any luck so far?
    – birgire
    Jun 10 '13 at 9:27
  • No, I ended up just hard coding for now Jun 11 '13 at 15:52
1

I'm not sure what you want, but if you are trying to get a unique <li>-list from the query you can try (untested):

<?php

// LOOP ARGUMENTS
$args = array( 'post_type' => 'xyz_members', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'tax_query' => array(array('taxonomy' => 'member_types','field' => 'slug','terms' => 'current-class','operator' => 'NOT IN')));
$loop = new WP_Query( $args );
$tags = array();

// COLLECT 
while ( $loop->have_posts() ) : $loop->the_post();
    $postTags = get_the_tags();
    if ($postTags):
        while($postTags as $tag):
            $tags[$tag->slug] = $tag->slug; 
        endwhile;
    endif;
endwhile; 

// OUTPUT
$years = range( 2006, 2020 );
foreach( $years as $year ):
    $year = (string) $year;
    if( isset( $tags[$year] ) )
         printf('<li id="_%s" class="filter on">%s</li>', $tags[$year], $tags[$year] );

endforeach;

?>

You could also consider array_unique() to remove duplicate values from an array.

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.