Forums › Forums › Search & Filter Pro › Filter Query Args
- This topic has 23 replies, 2 voices, and was last updated 8 years, 9 months ago by Ross.
-
Anonymous(Private) February 16, 2016 at 9:34 pm #37123
Hello Support,
I’m trying to filter search results based on a taxonomy item. I’m trying to this code that I found in the forum, but I can’t get it to work even just using $query_args[‘order’] = ‘desc’;What am I doing wrong here? Below is the the code I’ve been trying.
function sf_filter_query_args( $query_args, $sfid ) {
if($sfid==3906) {
$query_args[‘member-category’] = get_queried_object()->name;
//$query_args[‘order’] = ‘desc’;
}
return $query_args;
}add_filter( ‘sf_edit_query_args’, ‘sf_filter_query_args’, 10, 2 );
Anonymous(Private) February 16, 2016 at 11:48 pm #37151OK,
I’m able to get this to filter:function member_query_args( $query_args, $sfid ) {
if($sfid==3906) {
$query_args = array(
‘member-category’ => ‘hotels-motels’
);
}
return $query_args;
}How can I go about dynamically placing the value of member-category ?
add_filter( ‘sf_edit_query_args’, ‘member_query_args’, 10, 2 );
Ross Moderator(Private) February 16, 2016 at 11:58 pm #37160Hey there
It really depends on how you would detect the current “memeber-category”?
Thanks
Ross Moderator(Private) February 17, 2016 at 12:19 pm #37201Hi Christopher
So the question is, how do you detect the “current member-category”.
This is nothing to do with S&F – its something in your setup you need to figure out.
“How do I get the current member category in to a variable?”
Because once you can do this, you can add it to the code above:
function member_query_args( $query_args, $sfid ) { $current_member_category = get_current_member_category(); if(empty($current_member_category)) { // so some kind of sanity check to make sure we got one return $query_args; } if($sfid==3906) { $query_args = array( 'member-category' => $current_member_category ); } return $query_args; }
Now here, I made up a function
get_current_member_category
– this is something you would have to implement yourself – as I have no idea how to do something so specific in your environment.Thanks
Ross Moderator(Private) February 17, 2016 at 12:20 pm #37202Anonymous(Private) February 17, 2016 at 6:28 pm #37240Hi Ross,
thanks for all of the great input. Would this function run from the taxonomy-member-category.php template where the shortcode is placed or would it be happening on the search.php template? I believe the get_queried_object()->term_id; would only work if the query filter were taking place on the taxonomy-member-category.php template.Maybe it would work if the search never left taxonomy-member-category.php template. Is there a way of having search results display within the same page that the search is happening?
Anonymous(Private) February 17, 2016 at 6:40 pm #37242Ross,
I just tested this and found that if I display results using the search.php then the filter can’t use the get_queried_object()->name; , but if I display results using the shortcode method within the same taxonomy template, the get_queried_object()->name; is usable for the query filter.Here’s what I currently have:
function member_query_args( $query_args, $sfid ) { $current_member_category = get_queried_object()->name; if($sfid==3906) { $query_args = array( 'member-category' => $current_member_category ); } return $query_args; } add_filter( 'sf_edit_query_args', 'member_query_args', 10, 2 );
Anonymous(Private) February 17, 2016 at 8:23 pm #37257So the query filter only seems to work until a search is made or pagination is selected. See my link below. I am currently using S&F to display the posts.
As soon as you make a search, it no longer filters by the current member-category. In this example only posts in hotels and motels are being queried until the search takes place.
http://selkirkloop.org/wordpress/member-category/lodging/hotels-motels/
Anonymous(Private) February 17, 2016 at 10:33 pm #37268Update: pagination appears to be working.
If Member Regions taxonomy on http://selkirkloop.org/wordpress/member-category/lodging/hotels-motels/ is dropped down the accurate count shows until an option is selected. As soon as an option is selected, the search reverts back to searching all posts instead of the filtered $current_member_category .
-
AuthorPosts