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:
- How to display page content in a page template?
- How to query only for products with status “in stock” in WooCommerce? [closed]
- get_posts with multiple categories
- ajax category filter
- How to filter by category in REST API, excluding posts also in other category term?
- How to order category.php loop by ‘meta_value’?
- How can I display recent posts from a particular category in my header?
- Sorting Posts by custom field
- Single page theme
- Front End Sorting Questions
- WP Query with multiple categories – passing an array works?
- wp_query display posts from same category of the post
- WP_query category__in not working, only pulls from first category
- WP_Query order by date in meta_value
- How get posts from a subcategory of a category by name?
- 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
- Get image of latest post from taxonomies/categories
- 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
- WP_Query arguments to fetch custom post type posts which are in certain category?
- 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?
- How to show subcategories using loop?
- Pagination of a WP_Query Loop in a child-page page template
- list pages using page.php and NOT page-{slug}.php
- Remove a specific category ID from related post
- Complex Category selection as per user input
- Five posts from a category in footer
- How to get all unique categories for posts in loop?
- WordPress Pagination changes my template
- Display Featured Post by Categories and Avoid duplicated posts
- Cluster WooCommerce products in each Category Archive by Tags assigned to products
- category query for pages not working
- Search Query for multiple categories using ‘OR’ but having certain categories be ‘AND’
- add_filter(‘query_vars’) not working in custom template
- Check if loop has any categories?
- Inserting HTML to close and open divs in WP_Query loops
- Finding WordPress Posts assigned to multiple categories
- Combine results of multiple WP_Query to resemble single WP_Query
- WordPress Custom Search Form Displaying Unexpected Results
- wp_query is showing posts from other categories
- WP_Query Taxonomy categories filtering
- How to load a script code only in posts?
- inserting content of 1 Post to in another with a template hierarchy
- How to display a list of posts in same child category as current post
- How to exclude a category name from showing?
- WP_Query with multiple orderby NOT working with ASC and DESC, what’s wrong?
- Loop categories by recent post
- Get the child category ID of current category
- How to get posts by category and by choosing a taxonomy term?
- Related Posts Excluding Certain Categories
- Getting the permalink to the latest post from a category
- wp_query on page template not working (Genesis framework)
- Pagination for custom loop on custom page template is not displaying anything
- What is wrong with my WP_Query Arguments?
- How to create Page templates for showing Blog posts in different layouts?
- Custom Page Template Category loop not functioning correctly
- Post incorrectly excluded when using “category__in”?
- Related posts by current posts child category
- Filter products on category AND tag
- How to separate two columns from one content on a page template?
- How to use custom page for all posts with custom url, call another directory?
- query hook parse_tax_query function takes no effect
- How can I have sticky posts while ALSO showing posts from a specific category using one WP_Query?
- How to use the Term Object from a custom select field in a query
- 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?
- Related categories order posts by category
- INNER JOIN custom tables – SQL Query
- Pagination for Category does not work
- Display All Top Child Categories / Taxonomy
- Query post by Category and custom file (ACF)
- Pagination Shows Up But Won’t Work on Custom Gallery Page
- 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
- Filtering ‘Featured’ posts from a batch of category IDs
- Show posts from categories instead of tags
- Help displaying related categories
- how to avoid reloading/refresh the page when displaying the post of wp_list_categories
- Show full category tree for a year with all post titles?
- Fetching $_POST from Page Template into functions.php
- query.php – multiple is_category functions
- Create multiple sections for all categories and then queries all the posts for each of those categories
- Passing query variables to a custom page template
- When importing a database any page with a wp_query in doesn’t work
- Having trouble using this post category query on multiple pages?
- Related posts with WP_Query
- Adding css tweak based on page template
- Filter posts by category
- query_vars category_name only display one catgory out of multiple categories
- List categories using WP_Query
- Advanced Search – Is this possible?
- How to get post taxonomy url and name 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
- Excluding a category from frontpage but not from WP_Query