Reply To: Exclude Specific Pages in Search Results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Exclude Specific Pages in Search Results Reply To: Exclude Specific Pages in Search Results

#10279
Ernest Marcinko
Ernest Marcinko
Keymaster

Hi!

Thank you for your kind words!

Well, there is no option on the back-end to restrict to a certain set of pages – only excluding/including by certain categories or users (Advanced Options -> Exclude results panel), but since pages don’t have categories it won’t help.

I’ve looked up the code and I remembered that I have added a possibility to programatically restrict the results to a given set of IDs with a filter.

Try the following solution:

1. Make sure you have a back-up copy of your site just in case.
2. Try putting this custom code anywhere into functions.php file in your active theme directory:

add_filter('asp_query_args', 'asp_posts_only_in', 1, 1);

function asp_posts_only_in($args) {
  // Enter the page IDs here
  $page_ids = array(1, 2, 3, 4, 5, 6);  
  
  // Do not edit the code below
  if ( count($page_ids) > 0 )
    $args['post_in'] = $page_ids;
  
  return $args;
}

3. Enter the page IDs to the $page_ids variable, and that’s it. The search will only return results from the given page IDs 🙂

In case you have multiple search instances
.. and you only want to apply the change to one of them, then use this modified version of the code:

add_filter('asp_query_args', 'asp_posts_only_in', 1, 2);

function asp_posts_only_in($args, $id) {
  // Enter the page IDs here
  $page_ids = array(1, 2, 3, 4, 5, 6); 
  $search_id = 1; // change this to the search ID 

  // Do not edit the code below
  if ( $search_id != $id )
    return $args;
  
  if ( count($page_ids) > 0 )
    $args['post_in'] = $page_ids;
  
  return $args;
}

In this case, make sure to change the $search_id variable as well.

Best,
Ernest Marcinko

If you like my products, don't forget to rate them on codecanyon :)