1

I'm looking for widget or php query which I will be able to use as something like "on this day in history".

This query I want to show: Posts published in the past on the same day and month, from whole years of publication.

Thank you in advance for help.

2 Answers 2

1

You can use WP_Date_Query in conjunction with WP_Query to accomplish this.

$query = new \WP_Query( array(
    'posts_per_page' => 5,
    'date_query'     => array(
        array(
            'day'    => date( 'j', current_time( 'timestamp' ) ),
            'month'  => date( 'n', current_time( 'timestamp' ) ),
            'before' => 'this year'
        )
    )
) );

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
        // Do stuff.

    endwhile;
endif;
1

You can use this code to display posts related to specific date

$query_string = "order=ASC&posts_per_page=5";
function filter_where_date($where = '') {
$where .= " AND post_date = '2016-28-06'";
return $where;
}
add_filter( 'posts_where', 'filter_where_date' );
$custom_query = new WP_Query( $query_string );
while ( $custom_query->have_posts() ) {
$custom_query->the_post(); 

} // end while

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.