categories - Exclude a category and post_type from wp_query

I'm trying to figure out a simple way to exclude a particular category from a wp_query on my homepage.this partic

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 &amp; 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 &amp; 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 1
Add a comment  | 

1 Answer 1

Reset to default 0

You 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

相关推荐

  • categories - Exclude a category and post_type from wp_query

    I'm trying to figure out a simple way to exclude a particular category from a wp_query on my homepage.this partic

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信