Wordpress help - show all posts from same day and month

Associate
Joined
26 Apr 2012
Posts
1,187
I am working on a Wordpress site that shows "On this day" posts. So for example when I load the site it shows all posts that were published on the current day from any year. So for example when I load the page today it will show every post from 14th March regardless of year.

I have added the function "historical_posts_list" (See below) to Functions.php and this seems to work as expected. When I load the site it only shows posts from todays date for every year, Great !

Where I am stuck and looking for assistance is how I could add a date picker of some sort so that the user could then choose say January 1st and the page would update to show posts from the new date.

Hope that makes sense.

Any help appreciated. This is my first dive into Wordpress.

Code:
function historical_posts_list( $query ){
    if( $query->is_home() && $query->is_main_query() ){
        $date_query = array(
            array(
                'month' => date( 'n', current_time( 'timestamp' ) ),
                'day' => date( 'j', current_time( 'timestamp' ) )
            )
        );
        $query->set( 'date_query', $date_query );
    }
}
add_action( 'pre_get_posts', 'historical_posts_list' );
 
Associate
OP
Joined
26 Apr 2012
Posts
1,187
Many thanks I will look into your suggestions.

I am thinking of removing the changes from Functions.php and creating a new custom page with a child theme. As from what I have read when I update theme or wordpress any changes to say Functions.php will be overwritten. Does this sound correct?
 
Associate
OP
Joined
26 Apr 2012
Posts
1,187
Yep, if you're editing theme files you should make a child theme or they are likely to get overwritten when updated.

Wordpress is smart enough to look into the parent theme for any files that are not included in your child theme. If you create a child theme and dont include a functions.php file, it will automatically use the functions file from the parent theme. As soon as you create that file, it'll use the one from your child theme. This lets you make small customisations in a child theme whilst still using most of the code from the parent theme. You can update the parent theme without overwriting the edits you've made.

Excellent thank you.
 
Back
Top Bottom