Forums Forums Search & Filter Pro Meta tag Array

Tagged: 

Viewing 9 posts - 1 through 9 (of 9 total)
  • Anonymous
    #252506

    Hi peeps,

    I am using the “post” tab in the search&filter settings to display posts (pulled from Event Manager plugin) by default on loading to show them listed chronologically from most recent to less recent.

    In the “Post” tab I have these settings:

    – Default Order: Meta Value – descending
    – Choose Meta key: _event_start_date – Numerical

    The issue is that _event_start_date is not a date value, but it is an array and the date is stored in the first index array[0].

    Any idea on how to make the search&filter plugin take the first value of the array? rather than taking the whole array as a value, currently it is not working..

    Thanks

    Anonymous
    #252507

    Screenshot:
    Screenshot

    Anonymous
    #252511

    UPDATE – I see that the above is working properly in term of pulling the first index in the array, however ordering by Date if not working properly. any idea why?

    Trevor
    #252572

    The data will probably be stored as ‘serialized data’, which is the WordPress way to store an array. It isn’t the same structure as one might see in a PHP array, where each element is indexed as you suggest. There is no fixed structure for the data, but the plugin that stores the data will know how to interpret it and use it.

    You would need a single value date field in order for our plugin to be able to sort it correctly. Ideally, that custom field would store that date in YYYYMMDD format (so-called SQL date format, also used by plugins like ACF).

    I am puzzled as to why event plugins do not use this format, as it is very common in WordPress, or at least offer the option to store dates like this. As it is, it would require a second custom field with the date stored this way. It is possible that you could ‘autosave’ the value in this field using PHP in the child theme functions.php file to extract the date saved and convert it to the desired format. This post shows (in that case) how this is done to create a Year field (but the principal applies):

    https://support.searchandfilter.com/forums/topic/datefilter/page/3/#post-238869

    Anonymous
    #252605

    Thanks @Trevor. Nice idea!

    I have applied the code and made it work with a TEXT ACF to store the date on ‘save’ in this format as suggested: YYYYMMDD, however in the search settings I have selected the ACF field but the ordering is only numericalor Alphabetical.

    The events are now listed properly but only Ascending, descending is not working, I don’t think this solves the issue yet?

    Screenshot

    Anonymous
    #252611

    I added a new event to test this and it is not working, please look at this screenshot

    Anonymous
    #252613
    Anonymous
    #252619

    Sorry, I was using the wrong Meta field, the one with an underscore is the wrong one.
    Now that I selected the correct ACF field it is working! thanks.

    Here is the code I have used for reference:

    // Event Manager hack to store  well formatted date on save:
    
      function event_date_year_on_save( $post_id ) {
        $post_type = 'event'; //custom post type for events
      
        //Check if we are saving correct post type
        if( get_post_type( $post_id ) != $post_type)
          return;
      
        //Check it's not an auto save routine
        if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
          return;
        
        // get the event date of the post. Note this is in YYYYMMDD format
        $event_date = get_post_field ('_event_start_date', $post_id); // getting the meta field from Event Manager
      
        // remove "-" from the formatted date
        update_field( 'event_formatted_date_start', str_replace('-', '', $event_date), $post_id );
      }
      add_action('save_post', 'event_date_year_on_save');

    In the filter settings, I used Meta – Numerical ascending.

    Trevor
    #252623

    Thanks for getting back to me and sharing. I will close this thread for now.

Viewing 9 posts - 1 through 9 (of 9 total)