• The goal is to show today’s published posts or, if there’s none, the last day’s with posts published posts.

    I found?this old question, but the code suggested uses?query_posts. I’d like to use?WP_Query. I tried to scramble something, but it didn’t work:

        $current_year = date('Y');
        $current_month = date('m');
        $current_day=date('d');
    
        $today = getdate();
        $last_date = $wpdb->get_var("SELECT DATE(MAX(post_date)) FROM {$wpdb->posts} LIMIT 1");
        if (!empty($last_date)) {
        list($current_year,$current_month,$current_day) = explode('-',$last_date);
       } 
    
        $args = array(
            'date_query' => array(
                array(
                    'after'     => array(
                        'year'  => $today["year"],
                        'month' => $today["month"],
                        'day'   => $last_date,
                    ),
                    'inclusive' => true,
                ),
            ),
            'posts_per_page' => -1,
        );

    Any idea what’s wrong?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi @rghedin

    Thanks for posting. I took a stab at this and I hope it works out for you. I only tested on a small sample but it seems to work.

    A couple things to mention. I am not sure you posted the entire snippet but just in case, I will provide some deeper detail.

    • $wpdb needs to be globalized since you are using it as a means to direct query the latest post.
    • The query should be checking for post type as well as post status. You only want posts that are published and articles posted. WP handles custom post types and not defining the type could lead to returning something you don’t intend.
    • Instead of using after, a simple date_query can be used to get specific dates only. After could lead to returning posts from multiple days in certain cases.

    The snippet I came up with real quick is below. Please let me know if this is what you are looking for.

    global $wpdb;
    
    	/**
    	 * Get todays date and the last post date to compare
    	 */
    	$query_date = date( 'Y-m-d' );
    	$last_date = $wpdb->get_var( "SELECT DATE(MAX(post_date)) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'post' LIMIT 1" );
    
    	// Check if the last post is from today or not
    	if ( $last_date != $query_date ) {
    		$query_date = $last_date;
    	}
    
    	// Build the query
    	$args = array(
    		'date_query' => array(
    			array(
    				'year' => date( 'Y', strtotime( $query_date ) ),
    				'month' => date( 'n', strtotime( $query_date ) ),
    				'day' => date( 'j', strtotime( $query_date ) ),
    			)
    		),
    		'posts_per_page' => -1,
    	);
    
    	$p = new WP_Query( $args );
    
    	wp_send_json( $p->posts );

    The snippet will return only posts for a given day (today | last day a post was published).

    Thread Starter Rodrigo

    (@rghedin)

    Thanks, @justingreerbbi, it worked like a charm!

    There’s one issue, however: pagination doesn’t work. On /page/2/, for example, the same day/posts from home page are presented.

    I added the wp_reset_postdata(); after the loop (I guess this is necessary, right?), but it had no effect.

    Any idea what am I missing?

    Great news! Thanks for letting me know. I added it to my Gist so I have it for future ventures.

    As for the pagination, my guess is that the issue is how the snippet is being called and it interfering with the global WP_Query functionality.

    You could try to change the variable name of the query results to $wp_query. Maybe by calling the new WP_Query call, it is throwing the global variable off?.. This is just a guess though.

    If you are attempting to use pagination with this snippet you may have to modify it to handle the pagination parameters as well so that it does not go haywire and conflict with the global query.

    Try passing the paged value along with the date_query as well.

    • This reply was modified 9 months, 1 week ago by Justin Greer.

    Also, maybe try the wp_reset_query function at the end of the loop instead?

    Thread Starter Rodrigo

    (@rghedin)

    I was digging around and apparently there’s a known bug/issue with the_posts_navigation() and custom WP_Query class.

    Moderator bcworkz

    (@bcworkz)

    Not really a bug, definitely an issue. the_posts_pagination() cannot possibly know you want a custom query paginated. It’s your responsibility to paginate your own query. Like the SE reply indicated, if you need to paginate a custom query, you need to use paginate_links(). While there are lots of paginate_links() examples that get the requested page number from the “paged” query var, I strongly advise that you not do so. The query var belongs to the original main query, not your custom query. It doesn’t always have the value you think it should have.

    Instead pass your own page value in all of your pagination links. For example:
    example.com/page-slug/?my_page=2
    Then in your custom query, set the “paged” query var to the value from (int) $_GET['my_page']

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to display only posts from the last day with posts published?’ is closed to new replies.