The short of what I am trying to do here is: I want to display only one post based on a date stored in a custom field. I only want to display the post if it meets two conditions 1: The date needs to have not happened yet Ie must be larger than the current date 2: The date of the post to displayed has to be the closest to the current date out of all possible posts. Ie it's the next date to occur.
My problem is I can't figure out where to start when it comes to comparing one post to another then displaying only the appropriate one using the WordPress loop. I think I would need to use a for loop to go over each post and do my comparing then run a new query to display my one post.
TLDR; Can I use a forloop to go over a WP_Query? How can I access the post information in it?
Here is my Brainstoming code
<?php
$current_date = strtotime(date( 'F j, g:i a' ));
$soonest_date = 0;
$post_to_display = 0;
$posts = new WP_query($args); //will this work? Can I run a for loop over this?
//loop through all posts (imagining the posts are stored in a $posts array)
foreach( $posts as $post ) :
$post_date = srttotime(get_field('date')); //Does this need to be $post['date']? or something like that?
//Check if $soonest_date is set, if not then set it as the current post's date
if(! $soonest_date){
$soonest_date = $post_date;
}
//Check to see if the current post's date is larger than todays date and is less then the current soonest date
if($current_date < $post_date < $soonest_date ){
//if it is then this is the new soonest date and would be the one to be displayd if no smaller is found
$soonest_date = $post_date;
$post_to_display = the_ID(); // Again I think this might need to be something like $post['ID']?
}
?>
Thanks in advance! I tried to make this as clear as I could but i'm pretty confused :D
The short of what I am trying to do here is: I want to display only one post based on a date stored in a custom field. I only want to display the post if it meets two conditions 1: The date needs to have not happened yet Ie must be larger than the current date 2: The date of the post to displayed has to be the closest to the current date out of all possible posts. Ie it's the next date to occur.
My problem is I can't figure out where to start when it comes to comparing one post to another then displaying only the appropriate one using the WordPress loop. I think I would need to use a for loop to go over each post and do my comparing then run a new query to display my one post.
TLDR; Can I use a forloop to go over a WP_Query? How can I access the post information in it?
Here is my Brainstoming code
<?php
$current_date = strtotime(date( 'F j, g:i a' ));
$soonest_date = 0;
$post_to_display = 0;
$posts = new WP_query($args); //will this work? Can I run a for loop over this?
//loop through all posts (imagining the posts are stored in a $posts array)
foreach( $posts as $post ) :
$post_date = srttotime(get_field('date')); //Does this need to be $post['date']? or something like that?
//Check if $soonest_date is set, if not then set it as the current post's date
if(! $soonest_date){
$soonest_date = $post_date;
}
//Check to see if the current post's date is larger than todays date and is less then the current soonest date
if($current_date < $post_date < $soonest_date ){
//if it is then this is the new soonest date and would be the one to be displayd if no smaller is found
$soonest_date = $post_date;
$post_to_display = the_ID(); // Again I think this might need to be something like $post['ID']?
}
?>
Thanks in advance! I tried to make this as clear as I could but i'm pretty confused :D
Share Improve this question asked Jun 22, 2019 at 18:11 JoesLostJoesLost 31 bronze badge1 Answer
Reset to default 1Your query should be something like this:
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'meta_key' => '<ACF FIELD KEY HERE>',
'meta_value' => date( "Ymd" ),
'meta_compare' => '>',
'meta_query' => array(
'key' => '<ACF FIELD KEY HERE>',
'value' => date( "Ymd" ),
'compare' => '>'
),
'orderby' => 'meta_value_datetime',
'order' => 'ASC',
);
$queryPosts = new WP_query($args);
if($queryPosts->have_posts) {
while($queryPosts->have_posts) {
$queryPosts->the_post();
the_title();
}
}