Forums › Forums › Search & Filter Pro › Null Fields Not Returning Results
- This topic has 10 replies, 2 voices, and was last updated 7 years, 9 months ago by Trevor.
-
Anonymous(Private) March 22, 2017 at 11:43 am #98567
Hi,
Thanks for the great plugin, I’m just having a small issue and hoping you can help.
We have a search form that searches several fields within a post. For certain posts values are not completed.
The issue being that the range fields are being submitted as part of the search query regardless as to whether the user has interacted with them. The posts that do not include the range field at all are then not being returned.
e.g. one of the range fields is “Age” with the minimum set to 5 and the max set to 100. When the user alters another search field the “Age” variable is included in the search URL:
_sfm_age=5+70
The issue being any fields that do not have a value set for age are not returned at all.
Is there a way around this?
I can provide a link to the example via private message if this would help.
Thanks,
JoeTrevor(Private) March 22, 2017 at 2:36 pm #98669I understand the issue, and there is no simple way around the problem. In the next major version (V3) the range functionality will probably have a checkbox in settings to ‘Include NULL value posts in search results’.
For now all you can do is to intercept the search parameters, check if the range is at the default values, and remove that from the query, using PHP. See the Edit Query Arguments documentation (there may well be other snippets of the forum).
Anonymous(Private) March 22, 2017 at 3:15 pm #98705Sorry to bother you, I’m trying to modify the query, however the $query_args[‘meta_query’] is returning a blank array whenever I print it out. This is even after I have altered a field and submitted the form.
function remove_default_range( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==25 && $query_args) { if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query']))) { print_r($query_args['meta_query']); } } return $query_args; } add_filter( 'sf_edit_query_args', 'remove_default_range', 20, 2 );
Anonymous(Private) March 22, 2017 at 5:44 pm #98807Wooo, that got it. Thank you for your patience.
For anyone else with this issue please see my code below. Probably not perfect but it works. 🙂
// Exclude the default range values from the search as they are meaning the search returns no results function remove_default_range( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==25 && $query_args) { if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query']))) { global $searchandfilter; $sf_current_query = $searchandfilter->get(25)->current_query()->get_array(); /* echo "<pre>"; print_r($sf_current_query); echo "</pre>";*/ echo $sf_current_query['_sfm_minimum_per_round'][0]['value']; if($sf_current_query['_sfm_minimum_per_round'][0]['value'] == '0' && $sf_current_query['_sfm_minimum_per_round'][0]['value'] == '50000') { unset($sf_current_query['_sfm_minimum_per_round']); } if($sf_current_query['_sfm_minimum_per_season'][0]['value'] == '0' && $sf_current_query['_sfm_minimum_per_season'][0]['value'] == '1000000') { unset($sf_current_query['_sfm_minimum_per_season']); } } } return $query_args; } add_filter( 'sf_edit_query_args', 'remove_default_range', 20, 2 );
-
AuthorPosts