Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Fetching a URL parameter from within an asp_query_args callback function
- This topic has 5 replies, 2 voices, and was last updated 1 year, 5 months ago by
Ernest Marcinko.
-
AuthorPosts
-
December 9, 2024 at 6:37 pm #52202
Yans
ParticipantHi,
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!
December 10, 2024 at 9:29 am #52211Ernest Marcinko
KeymasterHi,
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.phphandler, 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_argsduring 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.
December 11, 2024 at 11:44 am #52239Yans
ParticipantThanks! 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";December 11, 2024 at 12:32 pm #52241Ernest Marcinko
KeymasterThe 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"December 12, 2024 at 8:34 am #52254Yans
ParticipantThanks Ernest.
Everything is working fine.
You can close this ticket.
December 12, 2024 at 8:39 am #52255Ernest Marcinko
KeymasterYou cannot access this content.
-
AuthorPosts
- You must be logged in to reply to this topic.