4

With the following code I can exclude all posts and pages that are top-level, but I would like to apply this only to pages, not to posts (but I still want all posts in the results):

function search_filter( $query )
{
    if(
        $query->is_search 
        AND $query->is_main_query()
        )
    {
        $query->set( 'post_parent__not_in', array( 0 ) );
        $query->set( 'post_type', array( 'post', 'page' ) );
    }
    return $query;
}

At the moment all my posts have 0 as post_parent, so they all get excluded from the results.

3
  • Did you try changing the post_type only to page instead of post and page like $query->set( 'post_type', array( 'page' ) ) Oct 30, 2013 at 12:34
  • You should take a closer look at the posts_where and/or posts_search filter. Oct 30, 2013 at 13:37
  • Any luck so far?
    – birgire
    Oct 31, 2013 at 15:26

1 Answer 1

1

I wonder if this will work for you:

function search_filter( $query )
{
    if( $query->is_search AND $query->is_main_query() )
    {
        // $query->set( 'post_parent__not_in', array( 0 ) ); // We comment this out here 
        $query->set( 'post_type', array( 'post', 'page' ) );
    }
    return $query;
}
add_action( 'pre_get_posts', 'search_filter' );

and

function wpse_120638( $where, $query ) 
{
    global $wpdb;

    if( $query->is_search AND $query->is_main_query() )
    {
            $from = "AND $wpdb->posts.post_type IN ('post', 'page')";
            $to   = "AND ( ( $wpdb->posts.post_parent NOT IN (0) 
                     AND $wpdb->posts.post_type IN ('page') ) 
                     OR $wpdb->posts.post_type IN ('post') ) ";

        $where = str_replace( $from, $to, $where );                                   
    }

    return $where;
}
add_filter( 'posts_where', 'wpse_120638', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.