2

I've created a wordpress blog locally hosted on a Raspberry Pi 4 4GB using a 500GB USB SSD. It works beautifully as an evernote replacement. I do have a problem with dating the posts however.

I want every post dated as accurately as possible based on original publishing dates of content (when known). For example, a book published in 1886 and made available on the public domain in PDF form I want to be able to post and date for December 31st 1886. Wordpress however doesn't allow dating before 1901.

Is there an override for this that will allow me to use 'ancient' dating whenever I prefer to?

  • Thank you for migrating this post, much appreciated :-) – David Moody Sep 5 '19 at 3:02
2

This is something totally new to me, so I did some research. What an exciting question!

I had a look at the how WP saves the date when a post is saved. WP runs post date through wp_checkdate(), which uses PHP native function checkdate(). According to the PHP docs checkdate() considers any year between 1 and 32767 valid, so 1800's shouldn't be a problem.

I tested the saving behaviour locally and created a post with a date of 1856-09-03. The date got saved correctly to the database, but it wasn't showing on the dashboard or the public view of the site.

I then look how (get_)the_date() works, in case you're using it in your page templates. It basically turns the post_date into unix timestamp, if I understood correctly, which is limited to 1901-2038 as explaned here, https://stackoverflow.com/questions/3985764/display-past-dates-before-the-year-1800. Luckily that very same Q&A also has the solution to the date problem - DateTime.

Here's how to replace default date formatting with DateTime in (get_)the_date().

add_filter( 'get_the_date', function($the_date, $d, $post){
  $date = new DateTime( $post->post_date );
  return $date->format('Y-m-d H:i:s'); // change formatting as needed
}, 10, 3 );

With that filter I got 1856-09-03 showing inside the post loop when using the_date().

To get the old dates showing on the posts list, you can do this,

add_filter( 'manage_posts_columns', function($columns){
  unset($columns['date']); // remove default date column
  $columns['real_date'] = __('Date'); // replace with custom column
  return $columns;
});

add_action( 'manage_posts_custom_column' , function( $column, $post_id ){
  if ( 'real_date' === $column ) {
    global $post;
    $date = new DateTime( $post->post_date );
    echo $date->format('Y-m-d H:i:s');
  }
}, 10, 2 );

add_filter( 'manage_edit-post_sortable_columns', function($columns){
  unset($columns['date']);
  $columns['real_date'] = __('Date'); // make custom date column sortable
  return $columns;
});

To change the post date shown on the post edit screen's Publish metabox, I think the only option is to use js/jquery as I didn't notice any filters related to it.

EDIT

And if you want to use the default date formatting you've set in the General Settings, you can get it with get_option( 'date_format' );. This way you don't need to hardcode the date formats to the filters above, e.g. 'Y-m-d H:i:s'.

| improve this answer | |
  • I think you Antti for your excitement! I will give your suggestions a go as soon as my teaching schedule is finished for the day! – David Moody Sep 5 '19 at 3:03
  • Hello, sorry, which file do I have to work with here? – David Moody Sep 5 '19 at 8:25
  • @DavidMoody Your (child) theme's functions.php file – Antti Koskinen Sep 6 '19 at 7:14
  • Hello, I've been trying to do what you show me above, and I'm not getting anywhere. I keep getting that pink bar saying 'Updating Failed' every time I try to input a pre-1901 date. I've even tried several different themes to see if that's the issue, with no success. I'm a novice at this kind of thing, so I don't know how to do much more than to just copy and paste code into the functions file as you suggested. – David Moody Sep 10 '19 at 4:11
  • @DavidMoody I did my testing with the classic editor so if you're using the Gutenberg Editor then there could be some differences how the date validation/saving works, but I can't say for sure. You could also try setting the date on the posts list with the Quick Edit, if you haven't already, and see if that works. But without knowing anything else about your site it's rather difficult to help you any further. You might want to consider hiring a developer to get the site working the way you want. – Antti Koskinen Sep 11 '19 at 6:22

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.