Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Using date_format on custom date field returns “now”
- This topic has 9 replies, 2 voices, and was last updated 1 year, 5 months ago by
Ernest Marcinko.
-
AuthorPosts
-
December 5, 2024 at 10:23 am #52155
Yans
ParticipantHi,
I am using the EventOn plugin which has a custom field called evcal_srow which is the event’s start date in UNIX timestamp format.
I can display the field in the results by using {evcal_srow} but it displays as a timestamp, this is normal,According to the ASP documentation I can format the custom date field like this:
{evcal_srow date_format=”F j, Y”}
However, it displays the current date (today) for all the results.
What can I do?
Thanks!
December 5, 2024 at 2:57 pm #52161Yans
ParticipantAnother question.
I am trying to limit the search results to upcoming events by adding a filter to limit results with evcal_srow greater or equal to now.
I adapted the code in the documentation:
add_filter('asp_query_args', 'asp_upcoming_events_filter', 10, 1); function asp_upcoming_events_filter( $args ) { $args['post_meta_filter'][] = array( 'key' => 'evcal_srow', 'value' => time(), 'operator' => '>=', 'allow_missing' => false ); return $args; }But it doesn’t have any effect.
How can I make sure the filter targets the specific search ID?Thanks!
December 5, 2024 at 3:34 pm #52162Ernest Marcinko
KeymasterHi,
What happens if you try it without the date format:
{evcal_srow}I’m asking because there might be a hook somewhere attached to the get_field function I suspect. If you get the same values, then it’s okay, if the timestamps change for each search, then that may indicate an issue somewhere.
Is is also strange that your custom code for filtering events does not work – that should definitely do something, it looks correct to me.
You can limit it to specific search IDs as well, with an additional argument:
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; }December 5, 2024 at 3:35 pm #52163Yans
ParticipantSorry to spam with messages.
The code above does work, there was a problem unrelated to ASP.
But, this code affects all my ASP instances. How can I target specific ASP instance?
Thanks
December 5, 2024 at 4:06 pm #52165Yans
ParticipantThanks for replying.
The filtering code is now working well and only for the specific ASP instance. Thanks.
The date formatting problem is still there and quite strange because it gives strange values, as you can see from the attached screenshot.
Using just {evcal_srow} displays the correct UNIX timestamp.
Using {evcal_srow date_format=’F j, Y’} (I switched ” with ‘) gives today’s date for all the different timestamps. For some results it gives strange year values like 6800, 7600.
December 6, 2024 at 10:13 am #52171Ernest Marcinko
KeymasterThat is very strange.
Can you add temporary back-end and sFTP access details? I may have to debug this locally. Testing on our servers are okay, so I suspect there is something I’m missing here.
December 6, 2024 at 1:45 pm #52176Yans
ParticipantUnfortunately I cannot give you access as the client is a European Agency and contractually I am not allowed to.
Is there another way to format that custom field? maybe via a function?
December 6, 2024 at 4:46 pm #52181Ernest Marcinko
KeymasterAll right, no problem. In that case let’s solve this via a custom code snippet.
First, make sure to use the field like this (without the date format, so it’s not processed):
{evcal_srow}Then use this custom code snippet:
add_filter('asp_cpt_advanced_field_value', function($value, $field_name){ if ( $field_name != 'evcal_srow' ) { return $value; } return date_i18n( "F j, Y", $value); }, 10,2);This will process the field afterwards, and hopefully should do the trick.
December 9, 2024 at 9:21 am #52190Yans
ParticipantHi Ernest,
Thanks for the code, it works!
This ticket can be closed.
December 9, 2024 at 10:00 am #52192Ernest Marcinko
KeymasterPerfect! In the meantime I found out why the date format was not working and made an issue on the tracker, it will be fixed in the upcoming release 🙂
If you like the plugin and have not rated already, feel free to leave a rating on the wordpress plugin repository, it’s greatly appreciated and helps us tremendously.
I am closing this ticket now.
-
AuthorPosts
- You must be logged in to reply to this topic.