Home > Back-end >  How can I retrieve the original post date of a page modified via wp_insert_post?
How can I retrieve the original post date of a page modified via wp_insert_post?

Time:08-25

On a Wordpress site, when updating an existing page using wp_insert_post, the get_the_date function appears to no longer show the original date.

The following code is in the page's template:

Posted by <a  href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>"><?php the_author(); ?></a> on <span class='date'> <?php echo get_the_date(); ?> </span>

<?php if (get_the_modified_date() > get_the_date()) : ?>
    <span >(last edited <?php echo get_the_modified_date(); ?>)</span>
<?php endif; ?>

This appears to work correctly if a page is edited in the admin panel. For example, a page that was originally posted on April 6th and edited on August 24th will say:

Posted by User on April 6, 2022 (last edited August 24, 2022)

If that same page is updated using wp_insert_post, it says:

Posted by User on August 24, 2022

It appears get_the_modified_date is returning the date the updated wp_insert_post function was used and not the date the page was originally posted on.

The code for wp_insert_post being used is:

$new_post = array(
    'ID'             => $postId,
    'post_name'      => $slug,
    'post_title'     => $title,
    'post_excerpt'   => $excerpt,
    'post_content'   => $description,
    'tags_input'     => explode(',', $_POST['post_tags']),
    'page_template'  => $template_page,
    'post_status'    => 'publish',
    'post_type'      => 'page',
    'post_parent'    => $parent_page,
    'comment_status' => 'open'
);

$pid = wp_insert_post($new_post);

The $postId supplied is the same ID as the original page. The content itself is correctly updated as expected, only the date the page was originally submitted appears to be incorrect.

Is there a way to display a page's original post date when a page is updated by this method?

CodePudding user response:

If you're updating the post then please use this:

$old_post = array(
    'ID'             => $postId,
    'post_name'      => $slug,
    'post_title'     => $title,
    'post_excerpt'   => $excerpt,
    'post_content'   => $description,
    'tags_input'     => explode(',', $_POST['post_tags']),
    'page_template'  => $template_page,
    'post_status'    => 'publish',
    'post_type'      => 'page',
    'post_parent'    => $parent_page,
    'comment_status' => 'open'
);

$pid = wp_update_post($old_post);

OR

$new_post = array(
    'ID'             => $postId,
    'post_name'      => $slug,
    'post_title'     => $title,
    'post_excerpt'   => $excerpt,
    'post_date'.     => $date, //Your OLD date here.
    'post_content'   => $description,
    'tags_input'     => explode(',', $_POST['post_tags']),
    'page_template'  => $template_page,
    'post_status'    => 'publish',
    'post_type'      => 'page',
    'post_parent'    => $parent_page,
    'comment_status' => 'open'
);

$pid = wp_insert_post($new_post);

You can get this old date in many ways like using get_the_date(date( 'Y-m-d H:i:s', $postId );

  • Related