Searching within given categories/taxonomy terms only

Solution #1 - via options (easy)

There is an option to restrict results to terms in the plugin back-end. Please check this documentation.

Solution #2 - Programatical restriction via custom code

For more advanced restrictions you can use the asp_query_args filter.
1
add_filter( 'asp_query_args', 'asp_include_only_term_ids', 2, 2 );
2
3
function asp_include_only_term_ids( $args, $id ) {
4
/**
5
* Enter the desired taxonomy=>terms here.
6
* For example, if you want to search category 1 and 2, then:
7
* "category" => "1,2"
8
*/
9
$include = array(
10
"category" => "1,2,3,4",
11
"post_tag" => "4,5,6,7"
12
);
13
// Allow results, that does not have connection with the taxonomies
14
$allow_empty = true;
15
16
// -- !! Do not change anything below this line !! --
17
if ( !is_array($args['post_tax_filter']) )
18
$args['post_tax_filter'] = array();
19
20
foreach ($include as $tax => $term_string) {
21
$terms = explode(",", $term_string);
22
foreach ($terms as $tk => &$tv)
23
$tv = trim($tv);
24
25
$args['post_tax_filter'][] = array(
26
'taxonomy' => $tax,
27
'include' => $terms
28
);
29
}
30
31
return $args;
32
}
Copied!
Last modified 1yr ago