最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pre get posts - problem with setting tax_query in pre_get_posts

programmeradmin3浏览0评论

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' );
发布评论

评论列表(0)

  1. 暂无评论