No not really – we don’t have much in terms of devs accessing our functions to do DB queries etc our way – so so the sf_edit_query_args is usually the way to go.
If your meta query requires a meta key to be an exact value, I can provide some code to set this in the query which should be much faster – its a bit clunky (we hope to improve this) but it taps in to our tables for a performance boost.
Let me know what you’re trying to do (share the snippet of code if you like)
Which takes an array of post IDs (the results S&F has found) and must return those posts.
You’ll then have to disable sorting and sort by post__in in the query (actually, you might be able to leave this set as “default” ordreing in your search form options), you can use the sf_edit_query_args filter to do this.
If you’re going to follow the link above exactly, the JOIN option might work for you anyway, but there’s two potential approaches for you to try.
How would I go about adding the To-date into the query? This is the query args that I get when I add the sf_edit_query_args filter (even with dates put in):
After scratching my head I managed to figure out the issue.
We perform our search outside of the regular WP_Query, so if you choose a category in our search form, WP doesn’t actually “know” its restrciting to a specific category, because our search does that, so WP appends the stick post…
I’ve managed to come up with some code, that add the WP category to the WP_Query (even though our plugin has already restricted the posts to this category), but now WP also knows that at a category restriction is happening, and excludes the sticky:
add_filter("sf_edit_query_args", "add_wp_category_to_query", 100, 2);
function add_wp_category_to_query($query_args, $sfid){
//if($sfid == 1234){ //you can restrict this behaviour to a search form ID
//get user category selection from the search form (ie, detect which categories have been selected)
global $searchandfilter;
$query_data = $searchandfilter->get($sfid)->current_query();
$query_data_array = $query_data->get_array();
if(isset($query_data_array['_sft_category'])){
$category_values = $query_data_array['_sft_category']['active_terms'];
$current_category_ids = array(); //this will contain all the IDs of the categories that have been selected in the search form
foreach($category_values as $category){
array_push($current_category_ids, $category['id']);
}
//restrict query to WP category
$query_args['category__in'] = $current_category_ids;
}
//}
return $query_args;
}
As there may be some small overhead when implementing this code, you can uncomment the first if( line, and restrict this to a specific search form / query.
I created a form that includes post meta fields. I would like it to also include posts that do not have these meta keys at all.
It is especially a problem because one might use other fields to filter the results, but once the form is submitted the meta fields (range sliders in that case) are taken into account as well, and posts with no meta disappear.
I tried to use the “sf_edit_query_args” filter, but it seems the meta_query is empty when it is applied…
maybe I am using it wrong?
So I’ve been thinking a bit about this, and obvously at some stage in the future we want to improve the way all this works, from within the plugin.
Let me just say, you can’t alter the query in the way you want using sf_edit_query_args – this is because we actually perform our search outside of the WP_Query, and insert the results… anyway, for now I see you have only two options going forward:
2) As you mentioned at the beginning, you wanted the results on page load, to match those after a search has been performed once the default range slider values are added. You can in fact do this, by going to the post meta tab in your search form, and adding two conditions in, to ensure all posts are above and below a specific values.
I hope that explains things a bit and gives you some viable options to work with!
In regards to your two issues which are both part of the same problem:
The way a form submits / resets is to include the sliders at their min / max value. We will try to address this in v3 within the plugin, but for now, if you want to have the same set of results when you reset as when you land on the page you have 2 options:
a) set a default value on all other posts – you can write a query to loop through all posts where there is no value for this post meta, and then auto add a value like 0
b) restict the meta query to the slider min/max params (you would have to use fixed min/max limits), you can do that from the post meta tab in your search form – this way the results that are shown when landing on the page, are teh same as when the form is reset / submitted, and the range slider params are added to the URL.
—
In regards to the filter: sf_edit_query_args, this is run before S&F applies its workings… S&F does a different kind of query, outside of your WP_Query, which is then combined with the WP_Query, so you can never truly unset that value. Your best bet would be to simply do unset($_GET['filter_name']); (you can dump this in the bottom of your functions.php to test) before S&F grabs the value and applies its sorting.
I’m a bit out of ideas at the moment… I have a search form for a custom post type where I also need to search meta fields of a custom taxonomy within this post type. Depending on what is chosen in the form, the search values for the taxonomy changes, so I can not add the information directly to the post type.
What I think about now as a possible solution is to alter the meta_query with filter “sf_edit_query_args”, use own non-dynamic select fields in the search form, make my queries in the function and just exclude posts with post__not_in.
I can add the select fields with jQuery or maybe manipulate a templates? But these fields also need to update the ajax with their GET values so that I can use them with the filter…
I hope you understand me and I can send you whatever you need. Maybe there is another way, too. I’m very open for any idea.
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!