• Hello!

    I would like to modify the default search query that it could be able to search only in pages and its own child pages.

    I mean, an user is browsing “Soccer” page. This page has a lot of subpages (“Team A”, “Team B” etc.). On the same level of “Soccer” I have also “Volleyball” and “Basket”, but I would like to show search results only of pages under the parent page.

    I’ve found this code:
    $query->query_vars['child_of']= parent_page_id;
    Where “parent_page_id” will be a Worpress function that retrieve the ID.

    But, I don’t know where to place that code to change automatically the search query when the search button is clicked.

    So, Where I have to place that code?

    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    It would go in a callback for a hook into ‘pre_get_posts’ action. What ever you do here will apply to all queries, so you would need to verify it is a search query by the ‘s’ query var having a search term.

    Also, please use this form of your code: $query->set('child_of', $parent_page_id );

    Either works, but the set() method is more future proof against possible changes to the class.

    Thread Starter Pepozzo

    (@pepozzo)

    Hello!

    I’ve added this function to my theme functions.php

    function SearchFilter($query) {
    if ($query->is_search) {
        global $post;
        $ancestors2 = $post->ancestors;
        // Check if page is a child page (any level)
        if ($ancestors2) {
        //  Grab the ID of top-level page from the tree
        $parent_page_id = end($ancestors2);
        } else {
    	// Page is the top level, so use  it's own id
        $parent_page_id = $post->ID;
        }
    	$query->set('child_of', $parent_page_id );
    }
    return $query;
    }
    add_filter('pre_get_posts','SearchFilter');

    But unfortunately it does not work.

    Moderator bcworkz

    (@bcworkz)

    I wouldn’t think it should matter, but try $wp_query->is_search() the method instead of the property. The method type casts the property as (bool) which makes me think the property could have non-boolean values.

    I’m unfamiliar with $post->ancestors, but if you’ve implemented something regarding this it may be fine. If things still do not work you’ll need to do a detailed debugging of the function, checking each value in turn until you find where things are not as expected. You may need to put in a die() call in order to see any var_dumps or print_rs. If you check each step you’ll eventually find where things go wrong.

    Alternately, you could error_log() values, then check the results in your error log file.

    Thread Starter Pepozzo

    (@pepozzo)

    Yes, I’ve already tried with is_search().

    If I try a var_dump, I can see “child_of” set with the correct ID, but, I really can’t understand why I continue to see all website results.

    So, this function seems to working fine, but the result is not so good. uhm.

    Moderator bcworkz

    (@bcworkz)

    Hmmm, you know what? I just assumed your reference was good. It’s not! (sounded right) There appears to be no “child_of” parameter. It should be “post_parent” to get all children of the ID assigned.

    Perhaps it was an old deprecated parameter? Can’t believe everything you find on the Internet, including here 😉 Sorry for my confusion, this should work for you now.

    Thread Starter Pepozzo

    (@pepozzo)

    Oh yes! I’ve discovered the same thing yesterday night!

    At the moment I’m using this code:

    function SearchFilter($query) {
    if (is_search() && !empty($_GET['s']) && !empty($_GET['childof'])) {
        $parent_page_id = intval($_GET['childof']);
    	$query->set('post_parent', $parent_page_id );
    }
    return $query;
    }
    add_filter('pre_get_posts','SearchFilter');

    and as you can see I’ve replaced with “post_parent”. But, now I’ve two problems:
    – it does not search in title of child pages.
    – it does not return some searched results. I mean: I’ve a page with a lot of content and in it I’ve “hello” and “goodbye”. If I search “hello” I’ve right results from searching, but if I try with “goodbye” I receive “content not found”. That’s an example, but I receive search results with some words, but no results with other words that are in the same page (obviously in the content).

    I really can’t understand why, again. 🙂

    Moderator bcworkz

    (@bcworkz)

    I suppose we’re making progress at least. You should be able to get the title search back. Compare dumps of the global $wp_query object, one taken from ‘pre_get_posts’ after your modification, and the other a straight search query that we know searches the titles. Whatever is missing, set it back in place.

    The erratic results are more perplexing. Try inspecting the actual query string sent to mySQL that’s passed as the first array element in an array parameter for the filter hook ‘posts_request’.

    If you correlate what you see here with what is seen in the query object, perhaps you may see some discrepancy that could be resolved. Beyond that, I’m not sure what else to investigate. Maybe deactivate all plugins, there may be one that’s corrupting the query without you knowing about it?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Modify default seach query’ is closed to new replies.