I created two fields:
TIME (for a third-party plugin, I don’t use the
I want to use
wp_query to display the posts in order of date (and time).
The problem is that I cannot merge these 2 data, this is either one or the other :
I tried to put an array but it also doesn’t work:
'orderby' => array('event_date' => 'ASC', 'event_time' => 'ASC')
Do you know if this is possible?
If this is not possible, when saving a post, will I be able to copy my 2 fields into a
Thanks for your help!
To do this you need to use meta_query and orderby with clauses. See 9th code example for orderby in the WP_Qeury doc https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
I could have miscounted 😛
It looks like this
$q = new WP_Query( array( 'meta_query' => array( 'relation' => 'AND', 'state_clause' => array( 'key' => 'state', 'value' => 'Wisconsin', ), 'city_clause' => array( 'key' => 'city', 'compare' => 'EXISTS', ), ), 'orderby' => array( 'city_clause' => 'ASC', 'state_clause' => 'DESC', ), ) );
Date fields store dates in the db in the form of “YYYYMMDD’ so you need to use “CHAR” or “NUMBER” as the field click.
I am not sure how time fields store values, you should check the DB to be sure.
But if you have the right data format then sorting by the two fields should work.
I had not seen the little subtlety!
It works! Thank you very much!
I put the code for people who would have the same problem:
'meta_query' => array( 'relation' => 'AND', 'date_clause' => array( 'key' => 'event_date', 'compare' => '=', ), 'time_clause' => array( 'key' => 'event_time', 'compare' => '=', ), ), 'orderby' => array( 'date_clause' => 'ASC', 'time_clause' => 'ASC', ),
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
The ACF site is currently experiencing some issues.— Advanced Custom Fields (@wp_acf) September 21, 2022
We apologize for the inconvenience and we are working hard to get it back up and running as soon as possible.
© 2022 Advanced Custom Fields.