Skip to main contentSkip to footer

Searching for all descendants of a page

If you want to restrict a search to a page and all it’s children, you can add a post_parent parameter to the search, and that’s it – but that only includes the direct children of the page. What if you want to include page, it’s children, all the grandchildren and their children? In that case, just using post_parent isn’t enough.

Let’s add a new search parameter, parent, that will include all descendants. WordPress has a helpful function here, get_post_ancestors(), which we can use to easily fetch the lineage of each post to only include posts that are descendants of our parent post.

This parameter is easiest to implement as a relevanssi_hits_filter filter function, so instead of limiting the search results to certain posts, we fetch everything and then weed out the unwanted posts.

/**
 * Introduces our `parent` query variable.
 */
add_filter( 'query_vars', function( $qv ) { return array_merge( $qv, array( 'parent' ) ); } );

/**
 * Filters the search results to only include the descendants.
 */
add_filter( 'relevanssi_hits_filter', 'rlv_parent_filter' );
function rlv_parent_filter( $hits ) {
    global $wp_query;
    if ( ! isset( $wp_query->query_vars['parent'] ) ) {
        // No parent parameter set, do nothing.
        return $hits;
    }
    $clan = array();
    foreach ( $hits[0] as $hit ) {
        // Loop through all the posts found.
        if ( $hit->ID === $wp_query->query_vars['parent'] ) {
            // The page itself.
            $clan[] = $hit;
        } elseif ( $hit->post_parent === $wp_query->query_vars['parent'] ) {
            // A direct descendant.
            $clan[] = $hit;
        } elseif ( $hit->post_parent > 0 ) {
            $ancestors = get_post_ancestors( $hit );
            if ( in_array( intval( $wp_query->query_vars['parent'] ), $ancestors, true ) ) {
                // One of the lower level descendants.
                $clan[] = $hit;
            }
        }
    }
    // Only include the filtered posts.
    $hits[0] = $clan;
    return $hits;
}

This solution works well in smaller databases (“small” meaning sites where an average search result set size is under 500 posts). In larger cases, this can get problematic: using a throttle may cause relevant posts go missing. If throttle leaves out some of the pages in the family tree, those cannot be found. On the other hand, without the throttle running get_post_ancestors() to all posts found can be a problem, especially if there are plenty of page hierarchies on your site.

In a larger database, it may be helpful to think this in some other way. I’m not sure what that would be, as building a MySQL query that would restrict the search using post_parent will also get very complicated.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Divi

Make Divi include all post types Divi search form module restricts the search to only posts and pages. To get rid of that restriction, you can add this to your site: add_action( ‘pre_get_posts’, function() { unset( $_GET[‘et_pb_searchform_submit’] ); }, 1 ); Divi and multisite searching Divi search results templates don’t……work in multisite searching. Multisite searching requires changes to the templates, and it’s impossible to make those changes in the Divi Blog module. If you want to do multisite searching with Divi, you need to use a standard WordPress search results template for the results. Getting Divi to show Relevanssi……excerpts Divi doesn’t use the default excerpts but instead truncates the beginning of the post and shows the first 270 characters. That means Relevanssi-generated excerpts don’t show up at all. However, you can configure Divi to show Relevanssi excerpts. Just enable the “Use excerpts when defined” setting in Divi settings,…

Polylang attachment searching
If Polylang is in use and you haven’t enabled “Media translation“, your attachment files won’t have a language. That’s fine,…
The search form shortcode

Relevanssi includes a search form shortcode that can be used to display a search form. The shortcode is searchform and it was introduced in Relevanssi Premium 2.0 and Relevanssi 4.0. Adding query parameters This shortcode prints out a basic search form. If you want to add additional query parameters, that’s……return $form; } Add this code to your site. Note this will then apply to all search forms, not just the ones generated by the shortcode. Versions 2.2.4 (Premium) and 4.1.3 (free) added a new filter hook, relevanssi_search_form, which does the same but only applies to the shortcode search form….…easy: just add parameters to the shortcode. Here are some examples: Search for: will restrict the search form to just products. Search for: will restrict the search form to posts, pages and news. Search for: will restrict the search form to categories 10, 14 and 17. Search for: will do…

Related Posts:

Comment Section:

2 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed