I am trying to list posts that do NOT have specific tags.
Currently, I am looping through each tag in a post, and if it's doesn't exist, list it. This works, except that it will list the same URL multiple times, if there are more than one tag.
<?php
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<ul>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<?php $post_tags = get_the_tags(); ?>
<?php foreach ($post_tags as $tag) : ?>
<?php if ($tag->name != "Tag1" and $tag->name != "Tag2" and $tag->name != "Tag3"): ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endif; ?>
<?php endforeach; ?>
<?php endwhile; ?>
<!-- end of the loop -->
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
For instance, a post with tags Fun
, and Learning
will list twice in a row. I just want this listed once.
Is there a way I can quickly check all tags in a post instead of looping?
Edit: I tried adding all the tags to a string, and using strpos()
but that seems to only check the first condition, and not the second/third:
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<?php $alltags = ""; ?>
<?php $post_tags = get_the_tags(); ?>
<?php foreach ($post_tags as $tag) : ?>
<?php $alltags .= $tag->name . " "; ?>
<?php endforeach; ?>
<?php if ((strpos($alltags, "SCA") == false) and (strpos($alltags, "SCAA") == false) and (strpos($alltags, "Tea and Coffee") == false)): ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> - <?php echo $alltags; ?></li>
<?php endif; ?>
<?php endwhile; ?>