0

EDIT : I answered myself.

Simple request for you. I am trying to develop a code that search for post for TODAY. If it returns nothing, then get the latest post before today.

For now, I have made a small algorithm to expose your my question.

<?php 
            // WP_Query arguments
            $args = array (
                'post_type'              => array( 'post' ),
                'year'                   => date('Y'),
                'monthnum'               => date('m'),
                'day'                    => date('d'),
            );

            // The Query
            $query = new WP_Query( $args );

            // The Loop
            if ( $query->have_posts() ) {
                while ( $query->have_posts() ) {
                    $query->the_post();
                    // do something
                }
            } else {
                // no posts found (Here I need to make the new request)

                $args_before = array (
                    'post_type'              => array( 'post' ),
                    'year'                   => date('Y'),
                    'monthnum'               => date('m'),
                    'day'                    => date('d'),
                );

                // Classic query will go here ...
            }

            // Restore original Post Data
            wp_reset_postdata();
?>

I start with my first array($args) that search for post for TODAY and then I check if I have results.

The tricky part, if I have no posts, do I need to work in the else{} and how can I acheive to get the previous first post before TODAY?

Do I need to work with BETWEEN and ORDER DESC ?

Any clue?

0

I think you should make a recursion function that will call itself if there's no posts found passing new date to query for..

function getDatePosts( $date, $query_args ) {

$query_args['date_query'] = array(
    'year' => date( 'Y', $date ),
    'mounth' => date( 'm', $date ),
    'day' => date( 'd', $date )
);

$query = new WP_Query( $query_args );

if( ! $query->have_posts() ) {
    $date = $date - ( 60 * 60 * 24 );
    $query = getDatePosts( $date, $query_args );
}

return $query;

}

And use it in your template:

$query = getDatePosts( time(), array( 'post_type' => 'post', 'posts_per_page' => 5 ) );
while( $query->have_posts() ) : $query->the_post();
 // output
endwhile;

Not tested, but should work.

| improve this answer | |
  • I will give a look at it! Thx for your collaboration. – Patrice Poliquin Sep 4 '15 at 17:09
0

I founded on this page something that helped me to complete my request.

How to get posts published between a date and today?

Use date_query

'date_query'            => array(
                            'column'        => 'post_date',
                            'after'         => '- 30 days'
                        ),

As you can see, we can check for column post_date and ask them to go backward.

            // WP_Query arguments
            $args = array (
                'post_type'              => array( 'post' ),
                'year'                   => date('Y'),
                'monthnum'               => date('m'),
                'day'                    => date('d'),
            );

            // The Query
            $query = new WP_Query( $args );

            // The Loop
            if ( $query->have_posts() ) {
                while ( $query->have_posts() ) {
                    $query->the_post();

                    // do something
                }
            } else {
                // no posts found
                $args_previous = array (
                    'post_status'           => 'publish',
                        'date_query'            => array(
                            'column'        => 'post_date',
                            'after'         => '- 30 days'
                        ),
                        'order'                     => 'DESC',
                    'orderby'                => 'date',
                    'posts_per_page'         => '1'
                );

                // The Query
                $query_previous = new WP_Query( $args_previous );

                if ( $query_previous->have_posts() ) {
                while ( $query_previous->have_posts() ) {
                        $query_previous->the_post();

                        // do something
                    }
                } else {
                    echo 'Really, you have no post!';
                }
            }

            // Restore original Post Data
            wp_reset_postdata();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.