Wordpress: WP_Query ordering anomaly - wordpress

I'm ordering a set of posts by an ACF date field using these arguments in WP_Query
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'date', //ACF date field
Which seems to work, except it's throwing an anomaly.
This is an echo of the list of dates (stored as Ymd)
20211221
20220118
20220125
20220201
20220208
20220215
20220222
20220301
...
20221122
20221129
20221206
20221213
20221220
20220111
The last one, which should be second, is appearing last. (they are weekly dates starting 21st December 2021

Try to use meta_value_date instead of meta_value as orderby parameter.
see also https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

Related

How to add a new sortable date meta to a custom post type

Looking for a point in the right direction, i've been working on an events plugin for a site, quite a simple plugin, adds a custom post type called events, adds some custom taxonomies, used ACF to add the fields to the CPT.
But I need to add 'start date and time', and 'end date and time' fields to it in a way that I'll be able to sort by start date/time in the admin side, and be able to sort by them when I query the posts as well.
I thought I might be able to do this with the ACF date/time field, but it's just not playing ball. Any ideas?
If i'm understanding correctly, you want to sort your results by a meta value right? that meta value being the date/time field. In your loop, you may want to try out something along the lines of:
$your_custom_query = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'date_time_meta_key_here',
'compare' => 'EXISTS',
)
),
'orderby' => 'date_clause'
));
See the original wordpress documentation here for more info: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

I cant get wordpress to sort advanced custom field sort by date

Hoi
i have a script, that have to order some post in wordpress. I youe Advanced Custom Fileds. The Field-group is called Kalender, and the date filed is called dato.
I try to order the list by this
$custom_query = new WP_Query(
array('posttype' => 'kalender',
'orderby'=> 'meta_value',
'meta_key'=> 'dato',
'order' => 'DESC'));
But that is not sorting the posts after date.
Can anyone tell me that is wrong?

Wordpress get_next_post based on custom date field

I have a Wordpress news site.
The news stories are added as posts.
To have more control I have a custom date field on the posts.
The posts are displayed in date order using the date in the custom field.
So the order the posts are displayed on the site can sometimes be different from the order of
the posts in the WP backend which are in the date they were added order.
On the single.php page I have a next and prev post links.
The next and prev post links use the order of the posts in WP which can be different from that on the front end.
Can I use the WP next,prev function with a custom date field instead of the actual WP order of posts. So the next and prev links will link to the next and prev links shown in the front end of the site.
I am not sure there is a solution using get_next_post(), but I guess you could query a single post with a higher/lower date using your custom meta.
Something like this should work, but I did not test it.
$args = array(
'meta_query'=> array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $currentPostDate,
'type' => 'DATE',
)
),
'posts_per_page' => 1
);
query_posts( $args );
Keep in mind this note from the documentation:
type (string) - Custom field type. Possible values are 'NUMERIC',
'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME',
'UNSIGNED'.
Default value is 'CHAR'. The 'type' DATE works with the
'compare' value BETWEEN only if the date is stored at the format
YYYY-MM-DD and tested with this format.

Wordpress: Custom-Field and multiple orderby

I have defined several custom post types (CPTs) with some Custom Fields (CF) and need to have a certain sorting - first by category, then by date, something along the lines of
$args = array (
'meta_value category' => 'ASC',
'meta_value startdate' => 'ASC'
),
'post_type' => 'training'
);
$the_query = new WP_Query($args);
However, this does not seem to work and the dates are not correctly parsed, since the default parsing of a meta_value is alphabetical. I cannot set the meta_type to DATE, because there are two meta_values.
Any ideas?
So, I was unable to find a solution. Instead I wrote a custom function in php that took the database result and sorted it to my needs.

Sorting problem with 'query_posts' function in wordpress. Sort by custom field not working

I am working with wordpress where i have a event listing system. There is a custom field in my post called starting_time which is unix timestamp. Now i want to short all posts by starting_time by this query_post command:
query_posts(array(
'post_type' => 'event',
'meta_key' => 'end_time',
'meta_compare' =>'>=',
'meta_value'=>time(),
'order_by' => 'start_date',
'order' => 'ASC'
));
But it not working. Here is the site http://citystir.com/events/. I am echoing the start_time in unix before the time remaining so you can see the value. And also my pagination is not working. May be I did something wrong in the query_post command.
You need to make sure that you have the correct meta_key name used in the query. You say that the custom field is called starting_time but you set order_by to start_date in your code.
The order seems ok at ASC as this will show next up event.
The problem was with the order_by command :) it is not order_by but orderby.

Resources