Forums › Forums › Search & Filter Pro › Meta tag Array
Tagged: meta key
- This topic has 8 replies, 2 voices, and was last updated 4 years, 5 months ago by Trevor.
-
Anonymous(Private) July 12, 2020 at 8:01 pm #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 – NumericalThe issue is that
_event_start_date
is not a date value, but it is an array and the date is stored in the first indexarray[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
Trevor(Private) July 13, 2020 at 12:14 pm #252572The 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(Private) July 13, 2020 at 2:33 pm #252605Thanks @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 onlynumerical
orAlphabetical
.The events are now listed properly but only Ascending, descending is not working, I don’t think this solves the issue yet?
Anonymous(Private) July 13, 2020 at 3:02 pm #252619Sorry, 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.
-
AuthorPosts