This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Exclude Specific Pages in Search Results

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #10278
    rosariagandarrosariagandar
    Participant

    Hi there,

    I am wanting to showcase only a select set of pages within the search results, specifically only pages that have the term ‘support’ in the url.

    Is this possible? Or is it possible to show only pages with a predefined ID? Please advise if this is an option – it’s a great plugin otherwise!

    Thanks

    #10279
    Ernest MarcinkoErnest 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:

    [php]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;
    }[/php]

    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:

    [php]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;
    }[/php]

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

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.