-
Search Results
-
Hello there,
I apologize if this has been asked before. I did some searching on these forums and thought I had found the start of a solution but it’s not working and I don’t know if it’s because I have the code wrong or the whole approach wrong. My situation:
I have a shop page that I’m building for a customer that has multiple product categories and subcategories. On the woocommerce archive page, I have a sidebar with a search and filter form that allows product search, category multi-select, price range, and sort order. It works excellent for ALL products, but I’m afraid the end user will get confused when they navigate to a product category via the main navigation, then try to use the search and filter form to search / filter JUST that category. Anytime you use the form it searches all products, which redirects to the main shop page.
My searches led me to the Edit Query Args function, which suggests that I could alter the function to have a conditional statement for whether or not the page was a product category, then filter the arguments for just that category. Here’s my code:
function ag_filter_category_search( $query_args, $sfid ) { if ( is_product_category() ) { $category = get_queried_object(); $cat_id = $category->term_id; //if search form ID = 225, the do something with this query if($sfid==4852) { $query_args = array ( 'product_cat' => $cat_id, ); } return $query_args; } } add_filter( 'sf_edit_query_args', 'ag_filter_category_search', 10, 2 );This is currently not doing anything that I can see, but it might be because the code itself is wrong. What I need to accomplish if possible (two options?):
1. When the user clicks to a category or subcategory page, have that automatically get added to the S&F form under the multiselect categories field. This way any further searches or filters will honor the current category (and the results will STAY on that category page rather than redirecting to the base “shop” page).
2. When the user is on a category page, change the S&F form to change the search query arguments to restrict results only to that category (and not leave the category page). Could this be something like filtering the URL that comes out of the search form? For example:
http://mysite.com/shop/?_sft_search_term=blahblah could be changed to:
http://mysite.com/product-category/category-name/?_sft_search_term=blahblahThe reason I’d not like to leave the category page is there is a custom display for each category that includes a category feature image and description.
Topic: Filter Query Args
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 );