Wordpress, filter posts if custom query variables are present. (pre_get_posts, add_vars)

我怕爱的太早我们不能终老 提交于 2019-12-08 07:01:01

问题


Basically, what I'm trying to do is filter my query if several variables are present, so that I can have something setup like this:

Posts Filter Mockup

I can do this with query_posts(), but I know that's terrible practice. So far, I've registered a couple of the query vars that I need to use, and I guess I just need to know how to include them in the query. Apparently what I'm using just doesn't work.

// Add query vars for filtering by specific category ids

add_filter('query_vars', 'add_vars');
function add_vars($new_query_vars) {
    $new_query_vars[] = 'speaker_id';
    $new_query_vars[] = 'book_id';
    return $new_query_vars;
}

// Filter posts if query vars are present

function vars_filter($query) {
    if ( !is_admin() && $query->is_main_query() ) {

        $query_book = get_query_var('book_id');
        $query_speaker = get_query_var('speaker_id');

        if(isset($query_book) || isset($query_speaker)) { 
            $cats = array($query_book, $query_book);

            $query->set('cat', implode(",", $cats));
        }   
    }
}

add_action('pre_get_posts','vars_filter');

I'm not exactly a pro at doing this kind of stuff. I've never used pre_get_posts before, so I'm a bit lost. Any help would be REALLY appreciated.

Thanks!

EDIT

I had also tried using this:

$query_book = $query->query['book_id'];
$query_speaker = $query->query['speaker_id'];

without much success...

来源:https://stackoverflow.com/questions/18149826/wordpress-filter-posts-if-custom-query-variables-are-present-pre-get-posts-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!