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.

add_filter( 'asp_query_args', 'asp_include_only_term_ids', 2, 2 );
  
function asp_include_only_term_ids( $args, $id ) {
  /**
   * Enter the desired taxonomy=>terms here.
   * For example, if you want to search category 1 and 2, then:
   *  "category" => "1,2"      
   */      
  $include = array(
    "category" => "1,2,3,4",
    "post_tag" => "4,5,6,7"
  );
  // Allow results, that does not have connection with the taxonomies
  $allow_empty = true;
  
  // -- !! Do not change anything below this line !! --
  if ( !is_array($args['post_tax_filter']) )
    $args['post_tax_filter'] = array();
    
  foreach ($include as $tax => $term_string) {
    $terms = explode(",", $term_string);
    foreach ($terms as $tk => &$tv)
      $tv = trim($tv);
    
    $args['post_tax_filter'][] = array(
      'taxonomy'  => $tax,
      'include'   => $terms
    );
  }
  
  return $args;
}

Last updated

Copyright Ernest Marcinko