Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Fetching a URL parameter from within an asp_query_args callback function › Reply To: Fetching a URL parameter from within an asp_query_args callback function
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.