The get_{$adjacent}_post_excluded_terms WordPress PHP filter is used to modify the IDs of terms excluded from adjacent post queries.
Usage
add_filter('get_next_post_excluded_terms', 'my_custom_excluded_terms');
add_filter('get_previous_post_excluded_terms', 'my_custom_excluded_terms');
function my_custom_excluded_terms($excluded_terms) {
// Your custom code here
return $excluded_terms;
}
Parameters
- $excluded_terms: int[]|string – An array of excluded term IDs or an empty string if none were provided.
More information
See WordPress Developer Resources:
Examples
Exclude specific categories
Exclude posts from categories with IDs 3 and 5.
add_filter('get_next_post_excluded_terms', 'exclude_specific_categories');
add_filter('get_previous_post_excluded_terms', 'exclude_specific_categories');
function exclude_specific_categories($excluded_terms) {
$excluded_terms = array(3, 5);
return $excluded_terms;
}
Exclude current post’s category
Exclude posts from the current post’s category.
add_filter('get_next_post_excluded_terms', 'exclude_current_category');
add_filter('get_previous_post_excluded_terms', 'exclude_current_category');
function exclude_current_category($excluded_terms) {
$current_category = wp_get_post_categories(get_the_ID());
return $current_category;
}
Exclude multiple categories
Exclude posts from multiple categories by providing an array of category IDs.
add_filter('get_next_post_excluded_terms', 'exclude_multiple_categories');
add_filter('get_previous_post_excluded_terms', 'exclude_multiple_categories');
function exclude_multiple_categories($excluded_terms) {
$excluded_terms = array(1, 2, 3, 4);
return $excluded_terms;
}
Exclude categories with a custom function
Exclude categories based on a custom function.
add_filter('get_next_post_excluded_terms', 'exclude_custom_function_categories');
add_filter('get_previous_post_excluded_terms', 'exclude_custom_function_categories');
function exclude_custom_function_categories($excluded_terms) {
$categories = get_categories();
$exclude = array();
foreach ($categories as $category) {
if (custom_function($category)) {
$exclude[] = $category->term_id;
}
}
return $exclude;
}
Reset excluded terms
Reset the excluded terms, allowing posts from all categories.
add_filter('get_next_post_excluded_terms', 'reset_excluded_terms');
add_filter('get_previous_post_excluded_terms', 'reset_excluded_terms');
function reset_excluded_terms($excluded_terms) {
$excluded_terms = array();
return $excluded_terms;
}