I'm trying to figure out a simple way to exclude a particular category from a wp_query on my homepage. this particular code is already excluding a post_type (videos) from displaying, and I'm not exactly sure how to implement code for excluding a category as well. Here's the current code:
<div data-st="st-six">
<div class="feed-heading"><h4>News & Updates</h4><a class="all" href="news/">Visit the News Page</a>
<hr/></div>
<?php $main_query = new WP_Query( array(
posts_per_page=>3,
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-video'
),
'operator' => 'NOT IN'
)
),
));
if ( $main_query->have_posts() ): ?>
<?php while ( $main_query->have_posts() ) : $main_query->the_post(); ?>
<div class="card">
<div class="meta"><?php the_time('F j, Y'); ?></div>
<h5 class="gamma"><a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a></h5>
<hr/>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
The category I want to exclude is "magazine" (slug name) with tag_ID = 44. any suggestions?
I'm trying to figure out a simple way to exclude a particular category from a wp_query on my homepage. this particular code is already excluding a post_type (videos) from displaying, and I'm not exactly sure how to implement code for excluding a category as well. Here's the current code:
<div data-st="st-six">
<div class="feed-heading"><h4>News & Updates</h4><a class="all" href="news/">Visit the News Page</a>
<hr/></div>
<?php $main_query = new WP_Query( array(
posts_per_page=>3,
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-video'
),
'operator' => 'NOT IN'
)
),
));
if ( $main_query->have_posts() ): ?>
<?php while ( $main_query->have_posts() ) : $main_query->the_post(); ?>
<div class="card">
<div class="meta"><?php the_time('F j, Y'); ?></div>
<h5 class="gamma"><a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a></h5>
<hr/>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
The category I want to exclude is "magazine" (slug name) with tag_ID = 44. any suggestions?
Share Improve this question asked Mar 11, 2020 at 16:25 DaveDave 11 Answer
Reset to default 0You can use simply category__not_in
parameter to exclude one or several categories from your result.
The sample is like this:
$query = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );
To see list of available parameters and complete example, see documentation of WP_Query in developer.wordpress
I hope you can use it.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744678041a4587448.html
评论列表(0条)