Help with asp_query_args() filter not querying meta_key vale as dates

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Help with asp_query_args() filter not querying meta_key vale as dates

This topic contains 3 replies, has 2 voices, and was last updated by Menzer92 Menzer92 4 years, 3 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #25342
    Menzer92
    Menzer92
    Participant

    Hi thank you for your plugin its brilliant however I have a problem, I am using the asp_query_args filter to filter by meta_key _EventStartDate which is Events calendar, I am using autopopulate so that the query displays upcoming events on page load, my code is below and what I notice is the meta_key value date keep converting to a number so I cannot filter out past events is there a way to achieve this?

    add_filter("asp_query_args", "asp_query_args_change", 1, 2);
    
    function asp_query_args_change($args, $search_id) {
      // Do your stuff with the $args array
      // ....  
    	if($search_id == '1'){
    	
    	  	$date = date('Y-m-d');
                    $args['post_meta_filter'] = array(
       			array(
                            'key'     => '_EventStartDate',
                            'value'   => $date,
            				'operator' => '>='
            		),
                    );
    	}
      return $args;
    }

    In other words any date input is converted to numeric and 2020-01-01 output result is 2018 so all I get are all the events from 2018 and not all the events from 2020-01-01

    Thank you for your help

    Menz

    • This topic was modified 4 years, 3 months ago by Menzer92 Menzer92. Reason: Added code
    #25344
    Menzer92
    Menzer92
    Participant

    Ok I have gone away and thought about other ways, as a self taught developer I have only one mindest and that is to find the solution if I can and when I cant I turn to the expert but I think I found the solution, however, can you let me know if this is ok to run, it gives me the results I want and filters nicely at this stage

    add_filter("asp_query_args", "asp_query_args_change", 1, 2);
    
    function asp_query_args_change($args, $search_id) {
      // Do your stuff with the $args array
      // ....  
      // Then return
    	
       $eventdate= date('Y-m-d');
       $loop = new WP_Query(array(
       'post_type' => array('tribe_events'),
       'posts_per_page' => -1,
       'orderby' => 'meta_value',
       'order' =>'DESC',
       'fields' => 'ids',
       'post_status' => 'publish',
       'meta_query' => array(
          array(
             'key' => '_EventStartDate',
             'value' => $eventdate,
             'compare' => '>=')
          )
       )
    );
    
    $upcoming_events = $loop->posts;
    
    wp_reset_postdata();
    	
    	if($search_id == '1'){
    	$args['post_in'] = $upcoming_events;
    	}
    
      return $args;
    }

    Hope this helps others

    #25348
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    That solution looks okay, but there is another way, so no WP_Query is required, saving some performance.

    The date filters are indeed not documented, sorry about that. The numeric operators automatically will force the value to a numeric. Can you please try this variation:

    add_filter("asp_query_args", "asp_query_args_change", 10, 2);
    function asp_query_args_change($args, $search_id) {
    	// Do your stuff with the $args array
    	// ....  
    	if($search_id == '1'){
    		$date = date('Y-m-d');
    		$args['post_meta_filter'] = array(
    			array(
    				'key'     => '_EventStartDate',
    				'value'   => $date,
    				/**
    				 * Operators that should work:
    				 * before, before_inc, after, after_inc, match, nomatch
    				 */
    				'operator' => 'after_inc'
    			)
    		);
    	}
    	return $args;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #25400
    Menzer92
    Menzer92
    Participant

    Hi I saw these options after looking in the code and tried them but they did not work for me, but I will try again and let you know.

    Really appreciate your help

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.