There are 3 problems here
It’s Using a Non-Standard Date Format
The dates are being stored like this:
20230131
Which is ok for us as we can interpret that as a date with no spaces, but it needs to stored like this:
2023 01 31
Specifically, it needs to be timestamp like this:
YYYY-MM-DD HH:MM:SS
or this:
YYYY-MM-DD
You can choose a different separator, and there is some flexibility over how the date is formatted, e.g. YY/MM/DD
also works, as does Y-M-D
, as long as the year comes before the month, and the month comes before the day.
You will need to convert all your existing data to use a consistent timestamp format.
It Does Not Use Date Based Ordering
Once the above change has been made, the date
oriented options become available, e.g. you could do this:
$args = [
'meta_query' => [
[
'key' => 'release_date',
'value' => array( '2019-01-01', '2019-12-31' ),
'type' => 'date',
'compare' => 'between'
]
]
];
$query = new WP_Query($args);
This also means you could do:
$args = [
'meta_key' => 'release_date',
'meta_type' => 'DATE',
'orderby' => 'meta_value',
];
$query = new WP_Query($args);
The Loop Check
In the main loop there is this code:
if ( get_field('dato-start') && ($today > $startDato) ) $unnagjort = TRUE;
if (!$unnagjort) {
get_template_part( 'template-parts/content', get_post_format() );
}
You’ve assumed because only 1 post is shown that the WP_Query
parameters are wrong, but actually, it’s more likely because of this check. If it fetches 10 posts but $unnagjort
is only false
once, it will show 1 post, how would you tell this apart from it fetching a single post? Even if there are more posts, it will only return them 10 or so at a time!
Instead, add a meta_query
to WP_Query
and make that do the work of filtering them out instead of doing it in PHP. Ask for all posts that have a start date earlier than today, for example:
$args = [
'meta_query' => [
[
'key' => 'release_date',
'value' => date('Y-M-D');,
'type' => 'date',
'compare' => '<'
]
]
];
Then your if
check can be eliminated, and it will only return the posts you want