You would use this in your child theme functions.php file, which should already have the opening PHP tag <?php, so you wouldn’t need that from the code, and it is NOT normal to close the PHP tags ?> at the end of the functions.php file as this can cause a site to crash.
Since the introduction of the cache, most queries are sent off to the cache DB, and they return a number of IDs… which are placed inside the post__in field in a query.
So, I’m actually scratching my head as to how we could do this.
function filter_function_name( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==225)
{
//modify $query_args here before returning it
$query_args['post__in'] = array(1,2,3,4);
}
return $query_args;
}
Which S&F also uses for setting up the query – and will probbably overeride this withe the results from the S&F cache.
Try initializing hook much later (so S&F has finished with the post__in var):
function filter_function_name( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==225)
{
//modify $query_args here before returning it
$query_args['orderby'] = 'title';
$query_args['order'] = 'DESC';
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 2 );
This means you should be able to access the object like:
function filter_function_name( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==225)
{
//modify $query_args here before returning it
//here you can limit the length of 's' which is the search term
$query_args['s'] = "overwritten search term";
}
return $query_args;
}
Ross,
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.
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 ) {
<?php
function sf_filter_query_args( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==225)
{
//modify $query_args here before returning it
//only show results within categories with these IDs
$query_args['cat'] = '2,6,17,38';
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'sf_filter_query_args', 10, 2 );
?>
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
We also use cookies to store items in your cart as well as allowing your to login on the site.
You can adjust all of your cookie settings by navigating the tabs on the left hand side.
By continuing to use this site, you also agree to our Privacy Policy.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
*Other cookies used for logging in and cart functions will only be used when you use those features and cannot be disabled.
If you disable these cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!