I’ve created a custom filtering facility for a front end search, which also includes a specific user group.

To add the function to the search I have used

add_filter('pre_get_posts','my_filter_the_search',10,1);

function my_filter_the_search($query){

    $post_type = filter_input(INPUT_GET, 'post_type', FILTER_SANITIZE_STRING);

    if($post_type == 'document'):

        add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

        //Get array of slugs of checked terms
        $terms = (array) $_GET['post_type'];



        //Tax_query array
        $tax_query = array(array(
            'taxonomy' => 'access',
            'terms' => 'basic-user',
            'field' => 'slug',
        ));


        //Tell the query to filter by tax
        $query->set('tax_query', $tax_query);

        return $query;

    endif;
}

This appears to be effecting the listing of a custom post type in the backend.

How do I only use this for the front end of the site?

2 Answers
2

Only hook the function if its not the backend.

if( !is_admin() ){
    add_filter('pre_get_posts','my_filter_the_search',10,1);
}

Leave a Reply

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