Home Forums General Issues Sort by date


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

    // 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 ): ?>
    	<?php foreach( $posts as $post ): 
    		setup_postdata( $post )
    			<a href="<?php the_permalink(); ?>"><?php the_title(); ?> (date: <?php the_field('post-date'); ?>)</a>
    	<?php endforeach; ?>
    	<?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.

  • I try it and doesn’t work

  • 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?


  • 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:

    I hope this helps.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Sort by date’ is closed to new replies.