Search only in the same category as the current post or page (single page)
There is no back-end option for this feature, but can be easily achieved by a small custom code. A similar code for archive pages can be found here.
1
add_filter( 'asp_query_args', 'asp_posts_from_same_cat', 10, 2 );
2
function asp_posts_from_same_cat($args, $search_id) {
3
$taxonomy = 'category'; // Enter the taxonomy name
4
5
// Do not change anything below
6
$categories = wp_get_post_terms( $args['_page_id'], $taxonomy, array('fields' => 'ids') );
7
if ( !is_wp_error($categories) && count($categories) ) {
8
$args['post_tax_filter'][] = array(
9
'taxonomy' => $taxonomy, // taxonomy name
10
'include' => $categories, // array of taxonomy term IDs to include
11
'exclude' => array(),
12
'allow_empty' => false // allow (empty) items with no connection to any of the taxonomy terms filter
13
);
14
}
15
16
return $args;
17
}
Copied!
Last modified 1yr ago
Copy link