This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Fetching a URL parameter from within an asp_query_args callback function

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Fetching a URL parameter from within an asp_query_args callback function

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #52202
    YansYans
    Participant

    Hi,

    I am using a script you sent me in my last ticket:

    add_filter('asp_query_args', 'asp_upcoming_events_filter', 10, 2);
    function asp_upcoming_events_filter( $args, $search_id ) {
    	// Do it only for search ID=1
    	if ( $search_id == 1 ) {
    		$args['post_meta_filter'][] = array(
    			'key' => 'evcal_srow',
    			'value' => time(),
    			'operator' => '>=',
    			'allow_missing' => false
    		);
    	}
    
    	return $args;
    }

    I want to change the operator from ‘>=’ to ‘<=’ depending on a URL parameter.

    This is the code I am using:

    // Registering the event_mode URL param with WordPress
    add_action('init','add_get_val');
    function add_get_val() { 
        global $wp;
        $wp->add_query_var('event_mode'); 
    }
    
    // Filtering by event start date so only events from today forward or backward (upcoming/past) will be displayed
    add_filter('asp_query_args', 'asp_upcoming_events_filter', 10, 2);
    function asp_upcoming_events_filter( $args, $search_id ) {
    	
    	if ( get_query_var('event_mode') == 'past' ) {
    		$filter_operator = '<=';
            } else {
    		$filter_operator = '>=';
    	}
    		
    	if ( $search_id == 2 ) {
    	    $args['post_meta_filter'][] = array(
                  'key' => 'evcal_srow',
                  'value' => time(),
                  'operator' => $filter_operator,
                  'allow_missing' => false
                );
    	}
        return $args;
    }

    For some reason it is not possible to fetch the event_mode URL parameter so the ‘if’ condition always executes the ‘else’ option.

    Is there something in asp_query_args that is blocking fetching the URL parameter?

    Thanks!

    #52211
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    The problem is that this is an ajax request, not the page rendering request. That query parameter does not exist in that context. When the live search request is sent, it goes to admin-ajax.php handler, and at that context, that is the request itself.

    This means, that if you want to change something based on the query variable, it has to be sent along with this ajax request as well. This is doable in a different way, by adding a variable to the form via a hook and using that in the asp_query_args during the ajax request later on.

    I made a quick custom code to show you how to handle that. I assumed there is a $_GET query argument ‘event_mode’, so based on that:

    /**
     * First, you need to add a new variable to the form, that is later sent
     * with the ajax search request.
     */
    add_action('asp_layout_in_form', function () {
    	$event_mode = $_GET['event_mode'] ?? 'past';
    	?>
    	<input type="hidden" name="asp_event_mode" value="<?php echo esc_attr($event_mode); ?>">
    	<?php
    });
    
    /**
     * The variable from the code above is sent with the $options['asp_event_mode'] in
     * the ajax request, so you need to check that.
     */
    add_filter('asp_query_args', function ( $args, $search_id, $options ) {
    	if ( isset($options['asp_event_mode']) ) {
    		$filter_operator = $options['asp_event_mode'] === 'past' ? '<=' : '>=';
    		if ( $search_id == 2 ) {
    			$args['post_meta_filter'][] = array(
    				'key'           => 'evcal_srow',
    				'value'         => time(),
    				'operator'      => $filter_operator,
    				'allow_missing' => false,
    			);
    		}
    	}
    	return $args;
    }, 10, 3);

    This may not be the solution, you may have to adjust it a bit, but it should be very close to it.

    #52239
    YansYans
    Participant

    Thanks! This works, only thing I had to change was:

    $event_mode = $_GET['event_mode'] ?? <strong>''</strong>;

    Otherwise it would always be set to ‘past’.

    The last thing I need is ordering. I saw in the documentation:

    $args['post_primary_order'] = "relevance DESC";

    But there is no mention for ordering by custom field. I would like to order descending by custom field evcal_srow.

    I did the following and it looks like it’s working, but wanted to confirm with you this is the correct code:

    $args['post_primary_order'] = "customfp DESC";
    $args['orderby_primary_cf'] = "evcal_srow";
    #52241
    Ernest MarcinkoErnest Marcinko
    Keymaster

    The core is almost correct, this is the one you are looking for:

    
    $args['post_primary_order'] = "customfp DESC";
    $args['_post_primary_order_metakey'] = "evcal_srow";
    $args['post_primary_order_metatype'] = "numeric"; // "string" or "numeric"
    
    #52254
    YansYans
    Participant

    Thanks Ernest.

    Everything is working fine.

    You can close this ticket.

    #52255
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.