Forums Forums Search & Filter Pro Post Meta Date Range

Viewing 10 posts - 1 through 10 (of 22 total)
  • Anonymous
    #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
    #88507

    You 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
    #89576

    Thank 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 );
    Trevor
    #89707

    From a coding POV, I can see no errors in that. What I don’t know is whether it would work, so I will have to ask Ross, the developer, if you have the principle and application correct.

    Anonymous
    #89712

    Thank you. And maybe you could also describe me correct setting for this.

    Trevor
    #89797
    This reply has been marked as private.
    Anonymous
    #89801

    Ok. Thank you.

    Anonymous
    #90286
    This reply has been marked as private.
    Trevor
    #90292

    He was not available for me to talk to yesterday, and ATM he is not either. I will try after lunch (it is just gone midday here).

    Ross Moderator
    #90518

    Hi 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

Viewing 10 posts - 1 through 10 (of 22 total)