Show Only One Category on WordPress Home Page
On this website, only the articles in the featured category appear on the home page. This is achieved by adding a filter to the child theme.
There are a number of ways to filter the content of the home page, but if a child theme is already being used, the method described here is almost trivial.
Add a filter
From the WordPress reference manual:
This hook is called after the query variable object is created, but before the actual query is run.
The following code snippet is added to the child theme’s functions.php file:
function my_home_filter( $query ) {
Ψif ($query->is_home() && $query->is_main_query())
ΨΨ$query->set(’cat’, get_cat_ID(’featured’));
}
add_filter(’pre_get_posts’, ’my_home_filter’);
The function causes the query object to be modified so that only articles in the featured category are returned. This is limited to the home page only.
The feeds are not affected, which is desirable, as there are already feeds which contain only one category.