admin管理员组文章数量:1429121
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; ?>
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; ?>
Share
Improve this question
edited May 10, 2019 at 21:23
BruceWayne
asked May 10, 2019 at 20:43
BruceWayneBruceWayne
1519 bronze badges
1 Answer
Reset to default 3I believe you can filter them out in your original WP_Query using Taxonomy Parameters. Then you can delete the for loop altogether.
$args = array(
'post_type' => 'post',
'posts_per_page' =>-1,
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => array('fun','learning'),
'operator' => 'NOT IN',
),
),
);
$wpb_all_query = new WP_Query( $args );
本文标签: phpList posts that don39t have a specific tag
版权声明:本文标题:php - List posts that don't have a specific tag? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745504691a2661191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论