Home › Forums › General Issues › Sort by date
hello
i need to display the today posts and tomorrow posts from a custom type (post-date). So if the today date is the same then show the posts.
i have this code
<?php
// get posts
$posts = get_posts(array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'post-date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?> (date: <?php the_field('post-date'); ?>)</a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I wanna display only the current date posts
Hi @pansotdev
I believe you can do it like this:
$posts = get_posts(array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'post-date',
'meta_value' => date("Ymd"),
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
Hope this helps.
Hi @pansotdev
I’ve just tested it on my end, and it was working as expected. Could you please share the JSON or XML export of your field group so I can test it out in my installation?
Thanks!
I solved
'meta_value' => date('d-m-Y'),
It was the wrong format.
Now i try to show the same results +1 day
Hi @pansotdev
I’m glad you solved it.
To get the next date, you can add 1 to the date and use “meta_query” to query multiple rules. This page should give you more idea about it: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters.
I hope this helps.
The topic ‘Sort by date’ is closed to new replies.
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.