Add Snippet To Project
When working with categories in WordPress, there may be times when you want to exclude certain subcategories from a loop. Whether it’s for organizing your content or improving navigation on your website, excluding subcategories can be a useful feature to have. In this article, we’ll explore how you can exclude subcategories in a loop and ensure that only the desired categories are displayed on your WordPress site.
function wpturbo_exclude_subcategories($query) {
// Get the current category ID
$cat_id = get_query_var('cat');
// Get the child categories of the current category
$child_cats = get_categories(array(
'child_of' => $cat_id,
'hide_empty' => false
));
// Exclude child categories from the query
$exclude_cats = array();
foreach ($child_cats as $child_cat) {
$exclude_cats[] = $child_cat->cat_ID;
}
$query->set('category__not_in', $exclude_cats);
}
add_action('pre_get_posts', 'wpturbo_exclude_subcategories');
The given snippet of code is used to exclude subcategories from a loop in WordPress. It allows you to filter out child categories and only display posts from the parent category.
To begin, a new function called wpturbo_exclude_subcategories()
is defined. This function will handle the exclusion of subcategories from the loop.
Within the function, the first step is to retrieve the current category ID using the get_query_var('cat')
function. This function returns the ID of the current category being queried.
Next, the child categories of the current category are fetched using the get_categories()
function. This function takes an array of arguments to define the query, including the child_of
parameter set to the current category ID and the hide_empty
parameter set to false
to include empty child categories in the results.
The child categories are stored in the $child_cats
array.
To exclude these child categories from the loop query, an empty array called $exclude_cats
is created.
A foreach loop is then used to iterate through each child category in the $child_cats
array. Inside the loop, the ID of each child category is added to the $exclude_cats
array using the cat_ID
property.
After looping through all the child categories, the $exclude_cats
array contains the IDs that need to be excluded.
Finally, the set()
method of the $query
object is called to set the category__not_in
parameter, which accepts an array of category IDs to exclude from the query. The $exclude_cats
array is passed as the value for this parameter, ensuring that the child categories are excluded from the loop.
The last step is to hook the wpturbo_exclude_subcategories()
function into the pre_get_posts
action. This action is triggered before the main query is executed, allowing us to modify the query parameters. By hooking our function into this action, we ensure that the exclusion of subcategories is applied to the main query.
In summary, this code snippet allows you to exclude subcategories from a loop in WordPress by retrieving the current category ID, fetching the child categories of the current category, and excluding those child categories from the loop using the set()
method of the query object.