show posts from specific categories

Show posts from specific categories on WordPress Blog Page

In WordPress, Blog Page by default displays the posts with the most recent posts being displayed first. However, sometimes there are situations in which we only need to show posts from specific categories on the Blog Page. For example, if you are not in favor of creating a custom post type for “Books”. In such cases, you will create a separate post category for Books and display it on a separate page. Obviously, you will not like to display both regular posts and books on the blog page.

We will discuss two methods to display Posts from only specific categories on WordPress Website.

Using WordPress Plugin

There is already a free plugin that allows you to exclude categories from the Blog Page. The plugin is available on the official WordPress website. You can download the plugin here. After excluding categories, the blog page will display posts from all other categories except the excluded ones.

Exclude categories from blog page

Without using WordPress Plugin

In case you are concerned about the performance of your website and do not want to add plugins then this part is for you. It is also helpful in the case if you have many categories that you can not exclude one by one using the plugin.

Just add the below function in the Functions.php file of your theme. Don’t forget to replace ‘4’ with the category ID that you want to show on the Blog Page. If you do not know how to find category id, check the article “How to Find Post, Category, Tag, Comments, or User ID in WordPress” from wpbeginner.

function category_to_display( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '4');
}
}
add_action( 'pre_get_posts', 'category_to_display' );

If you want to display posts from more than one category on the blog page then you can separate multiple ids with commas. Just check the code below for showing multiple specific categories.



function category_to_display( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '4, 8, 12');
}
}
add_action( 'pre_get_posts', 'category_to_display' );

You can replace ‘4, 8, 12’ with category ids that you want to show on the Blog page of the WordPress website.

I hope this article helped you learn how to show posts from specific categories on a Blog page. If you have any questions or need more customization, you can let me know in the comments. You can also Subscribe to my Youtube Channel where I will post some helpful tutorials. If you want me to write about any specific customization in WordPress, you can let me know in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *