I tried to make an advanced search which shows posts in a custom post type (book) with custom taxonomy (writer) that user wants. This code works in a query in a page but didn't work in pre_get_posts. whats the problem?
function advanced_search_query( $query ) {
if ( isset( $_REQUEST['search'] ) && $_REQUEST['search'] == 'advanced' && ! is_admin() && $query->is_search && $query->is_main_query() ) {
$query->set( 'post_type', 'book' );
$search_case = $_GET['book-writer'];
$tax_query = array(
array(
'taxonomy' => 'writer',
'field' => 'name',
'terms' => $search_case,
)
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'advanced_search_query' );