问题
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