Forums › Forums › Search & Filter Pro › Post Meta Date Range
- This topic has 21 replies, 4 voices, and was last updated 6 years, 9 months ago by Ross.
-
Anonymous(Private) February 6, 2017 at 8:35 pm #88394
Hi,
I have a custom type post with “start” and “end” date custom fields (acf pro).
In results posts are displayed only when two dates selected in date range (start and end date).
How can I make it work even if only one date is selected?
Just how it works with a post date range.Trevor(Private) February 7, 2017 at 3:51 pm #88507You want to give the user the option to either add one date only, OR both dates?
In the instance of them entering only one date, Auto Count (on user interaction with the form) would not work, BUT you could, with some custom coding, use our Edit Query Arguments filter.
You would have to create logic to fetch the two date fields from the query, check to see if both to and from dates are present, and if not, edit the missing date to be the same as the completed date.
Anonymous(Private) February 11, 2017 at 11:45 pm #89576Thank for your answer. Do you have a code sample? I tried some but no result.
function filter_function_name( $query_args, $sfid ) { if( $sfid == 940 ) { global $searchandfilter; $sf_current_query = $searchandfilter->get(940)->current_query(); $array_data = $sf_current_query->get_array(); $tender_start = $array_data['_sfm_tender_start']['active_terms'][0]['value']; $tender_end = $array_data['_sfm_tender_end']['active_terms'][0]['value']; $args = array( 'meta_key' => 'tender_start', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'tender_start', 'compare' => '>=', 'value' => $tender_start ), array( 'key' => 'tender_end', 'compare' => '<=', 'value' => $tender_end ) ) ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Ross Moderator(Private) February 16, 2017 at 4:09 am #90518Hi Max
I had a look, it seems to me there is something wrong with your code sample.
The filter receives a
$query_args
object, which contains the wp_query args that S&F defines, and at the end of the function it needs to return this$query_args
object.Inside the function you are free to modify this as you want, which it looks like you got close to do doing but not quite there..
You add your meta query to a new variable called
$args
, which never gets used.I would do it like the following (untested – please also copy & paste into editor to see my comments, this is a bit clunky):
<?php function filter_function_name( $query_args, $sfid ) { if( $sfid == 940 ) { // I would use some known values for testing (I just made some dates up here) $tender_start = "20170101"; $tender_end = "20170101"; /*global $searchandfilter; $sf_current_query = $searchandfilter->get(940)->current_query(); $array_data = $sf_current_query->get_array(); $tender_start = $array_data['_sfm_tender_start']['active_terms'][0]['value']; $tender_end = $array_data['_sfm_tender_end']['active_terms'][0]['value'];*/ //if we want to play nice and not completely replace the existing meta query args //there might be, then we need to check if some meta queries already exist and treat it differently $meta_query = array( 'relation' => 'AND', array( 'key' => 'tender_start', 'compare' => '>=', 'value' => $tender_start ), array( 'key' => 'tender_end', 'compare' => '<=', 'value' => $tender_end ) ); if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query']))) {//this means there are some meta queries already here, so push this one onto the end array_push($query_args['meta_query'], $meta_query); } else {//else there aren't any meta queries //$query_args['meta_query'] should always be an array of meta queries according to wp docs $query_args['meta_query'] = array($meta_query); } // not sure if you need these, you should set the order via the S&F admin (in the <code>posts</code> tab) $query_args['meta_key'] = 'tender_start'; $query_args['orderby'] = 'meta_value_num'; $query_args['order'] = 'ASC'; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
I’ve written that in my editor so it should be correct syntax, but I haven’t actually tested it, its just showing you how to properly use the $query_args to make the changes you want.
Hope that helps
-
AuthorPosts