Home › Forums › General Issues › using both published date and date picker to sort posts
I would like to sort posts using firstly the custom field date picker by ACF plugin for wordpress, and then when that’s non existing or empty, by the published date.
Our theme works with cases so I cooked up something like this but I failed misearbly, any suggestions ?
The reason for this is because we are currently uploading a lot of old concert reports, so published date should be kept on today (the easiest given the vast amount of data yet to be uploaded), where the concert date is to be set manually.
The ACF field “datum” is then later used in the title of the posts to help generalize the way dates are displayed.
case 'concertdatum': $wp_query_args['meta_query'] = array( 'relation' => 'AND', 'datum_clause' => array( 'meta_key' => 'datum', 'meta_type' => 'DATETIME' ), 'publish_clause' => array( 'column' => 'post_date_gmt', 'compare' => 'EXISTS', ), ) $wp_query_args['order'] = array( 'datum_clause' => 'DESC', 'publish_clause' => 'DESC' ); break;
A working bit of code, sorting only by the custom field looks like this:
case 'concertdatum': $wp_query_args['meta_key'] = 'datum'; $wp_query_args['meta_type'] = 'DATETIME'; $wp_query_args['orderby'] = 'meta_value'; $wp_query_args['order'] = 'DESC'; break;
post_date_gmt is not a meta field. Remove it from your meta query.
WP does not have an option to order posts by the post_date_gmt value, only date or “post_date”
Also, ACF does not store date fields as a date. Use number of string, and you need to set a compare or value
'datum_clause' => array( 'meta_key' => 'datum', 'compare' => 'EXISTS' ),
Then in your order by use
$wp_query_args['orderby'] = array( 'datum_clause' => 'DESC', 'date' => 'DESC' );
Currently I have this but it still puts the published date first, unless I misunderstood you and did something wrong of course…
case 'concertdatum2': $wp_query_args['meta_query'] = array( 'datum_clause' => array('meta_key' => 'datum', 'compare' => 'EXISTS') ); $wp_query_args['orderby'] = array('datum_clause' => 'DESC', 'date' => 'DESC'); break;
As far as I know it should be working.
It should be ordering the posts by the custom date field. If two of those fields have the same date then those should be ordered according to the post date.
You also have to remember that an ACF date field does not store a time. So today would be in the DB like this
20181211 Two posts that have today’s date in the custom field will be ordered by the post date, which includes a timestamp, for example
Ah, so if the custom field is a date time field, which upon closer look it is, I need to alter the code probably ?
turns out it needed to be post_date instead of simply date, this is the working code.
case 'concertdatum': $wp_query_args['meta_query'] = array( 'datum_clause' => array('key' => 'datum','compare' => 'EXISTS') ); $wp_query_args['orderby'] = array('datum_clause' => 'DESC', 'post_date' => 'DESC'); break;
For some odd reason however, the mega menu that my theme uses refuses to sort the posts like I request it to, but that’s a different question not for this site 😉
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 most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.