Use pre_get_posts to exclude the categories you don’t want to display in the loop.
function exclude_posts_from_specific_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-2' );
}
}
add_action( 'pre_get_posts', 'exclude_posts_from_specific_category' );
Or create a new WP_Query and use the Category Parameters.
$args = array(
'category__not_in' => 2 ,
'category__in' => 4
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
}
wp_reset_postdata();
If you only want to display posts from one category, you would use the category archive.
Related Posts:
- Exclude or Include category ids in WP_Query
- How to query only for products with status “in stock” in WooCommerce? [closed]
- WP_Query to show post from a category OR custom field
- get_posts with multiple categories
- ajax category filter
- Display recent posts from the same category as current post in sidebar
- Counting number of posts with Category B in Category A
- Use Transient API to cache queries for all posts in all categories?
- Sorting Posts by custom field
- Single page theme
- Single page theme that uses pages for the content
- WP Query with multiple categories – passing an array works?
- wp_query display posts from same category of the post
- Display Posts of a Category in Alphabetical Order (Custom Post Type)
- WP_query category__in not working, only pulls from first category
- WP_Query order by date in meta_value
- What are the differences between “Latest Posts” and “Static Page”?
- get complex results set according to category structure
- Searching through different categories on different pages code is not working
- How to select posts from one category but exclude posts in another category?
- WP_Query doesn’t get sticky post at the top, when used in a page template
- Get Category Archive Template Name Dynamically
- How do I reset this wp_list_categories query?
- How can you get first post, last post and post count in a category?
- How to get count of posts assigned to given category?
- Pagination of a WP_Query Loop in a child-page page template
- list pages using page.php and NOT page-{slug}.php
- List of the years with posts presented
- Get pagination working for custom loops within page templates
- WordPress Pagination changes my template
- Category applied to pages, creates multiple breadcrumb entries after a search query (On the translated site)
- category query for pages not working
- Query posts from category A, and from either category B or C
- Combining categories (Query posts with multiple taxonomy terms)
- Check if loop has any categories?
- Filtering out child category posts from parent category archive not working
- Limiting number of related posts
- Category ‘pad_counts’ & ‘parent’ conflict
- Pass $page_id to new WP_Query
- Include posts from some categories while excluding from others
- Create a page template for “top rated posts” but show full content and not just a list
- WP_Query Taxonomy categories filtering
- Hide products in uncategorized category from search results
- WP_Query returns different results from get_posts() [closed]
- How to load a script code only in posts?
- inserting content of 1 Post to in another with a template hierarchy
- How to echo woocommerce category name
- Slider won’t work with custom query
- How to generate feed for custom template
- WP_Query with one category in args shows other categories
- WP_Query with multiple orderby NOT working with ASC and DESC, what’s wrong?
- How order posts from category by date and comment count?
- Get the child category ID of current category
- Related Posts Excluding Certain Categories
- Pagination for custom loop on custom page template is not displaying anything
- Custom Page Template Category loop not functioning correctly
- Page template doesn’t display simple HTML if inserted into other page?
- Post incorrectly excluded when using “category__in”?
- Filter sub-category from checkbox form
- `offset` WP_Query argument dont work via `pre_get_posts`
- Use get_cat_ID to retreive multiple category IDs
- Function the_posts_pagination() not compatible with WP_Query arguments
- adding pagination to a foreach loop in wordpress
- How to use custom page for all posts with custom url, call another directory?
- query hook parse_tax_query function takes no effect
- How to use the Term Object from a custom select field in a query
- 3 posts from each existing category on one page
- How to display the category dropdown auto search list when key press?
- Categories In English version showing not canonical URL, instead shows query search result
- What code to use in an array to call the current sub-category?
- Get categories within specific term
- INNER JOIN custom tables – SQL Query
- How to show specify category template for both parent and child category
- Pagination for Category does not work
- Exclude parent categories from recent posts list
- Check the product in the cart from which category is and show message
- WP_Query showing all posts, except from category X, unless it’s also in Y
- Single query for multiple categories
- Display 3 levels of categories on page
- Multiple values in WP_Query : category__and
- I have 3 categories, i want to display on a loop the last 3 of every category
- Adding Category in WP_Query Not Working
- display all posts from category with and without terms in chronological order
- Help displaying related categories
- Pull posts from all categories if quantity is not met?
- How can I use a specific wordpress page template if certain words are used in page title [closed]
- how to avoid reloading/refresh the page when displaying the post of wp_list_categories
- Category Archive not working for pages
- Show full category tree for a year with all post titles?
- Using URL variables on a custom WP_Query
- Display Count of posts
- Display post list within category list sorted by name [duplicate]
- Pagination is not working on custom query inside a custom home page template
- Related posts with WP_Query
- WordPress pagination returns the same posts
- Main loop querying current template’s info only in custom category archive pages, not my posts
- query_vars category_name only display one catgory out of multiple categories
- Pagination only showed when no category is set in wp_query
- Using WP Query, I want to include all posts in category 1 as long as they are not also in category 2
- How do I show related posts from categories instead of tags?