Wordpress help - show all posts from same day and month

Associate
Joined
26 Apr 2012
Posts
1,237
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' );
 
You can simply send a querystring to the file like
Code:
?month=12&day=25

Code:
if (isset($_GET['month']) && ctype_digit($_GET['month'])) {
    $month = $_GET['month'];
}
else {
  $month = date('n', current_time('timestamp'));
}
if (isset($_GET['day']) && ctype_digit($_GET['day'])) {
    $day = $_GET['day'];
}
else {
  $day = date('j', current_time('timestamp'));
}

update this part
Code:
           array(
                'month' => $month,
                'day' => $day
            )
 
Last edited:
I agree with Kwerk, that is the simplest way to do it.

I would probably do it using a post variable rather than a querystring to make it easier on the other end where you are sending the data. I'd use the jquery ui datepicker control in a form to post a date.
Rather than $_GET['month'] and $_GET['day'], I would have $_POST['date'] then extract the month and day from the date.
 
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?
 
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.
 
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