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

Using date_format on custom date field returns “now”

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Using date_format on custom date field returns “now”

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #52155
    YansYans
    Participant

    Hi,
    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!

    #52161
    YansYans
    Participant

    Another 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!

    #52162
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    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;
    }
    #52163
    YansYans
    Participant

    Sorry 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

    #52165
    YansYans
    Participant

    Thanks 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.

    #52171
    Ernest MarcinkoErnest Marcinko
    Keymaster

    That 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.

    #52176
    YansYans
    Participant

    Unfortunately 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?

    #52181
    Ernest MarcinkoErnest Marcinko
    Keymaster

    All 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.

    #52190
    YansYans
    Participant

    Hi Ernest,

    Thanks for the code, it works!

    This ticket can be closed.

    #52192
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Perfect! 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.

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