0

I have a code.

add_action( 'init', 'njengah_change_post_date_programmatically');
function njengah_change_post_date_programmatically(){
$time = current_time('mysql');
 wp_update_post(
          array (
                      'ID' => 838, // ID of the post to update
                      'post_date' => $time,
                      'post_date_gmt' => get_gmt_from_date( $time )
                     )
            );
      }

But this code only work on function php.

The idea is, I want retrieve the ID from post meta in my current post. The post date will retrieve from current post. And the post is the latest post.

4
  • I'd guess that running this in single.php is too late: your site will already have a copy of the post in memory and working with that.
    – Rup
    Commented Dec 20, 2022 at 12:24
  • 3
    However this feels really wrong, and I don't completely understand what you're trying to do. You want to make this one specific post ID always the most recent post? There must be better ways to do that without making a database edit on every page load.
    – Rup
    Commented Dec 20, 2022 at 12:26
  • well simply put, the meta post id is id of a post-type. What I want to achieve is, when the post is published, the post-type will have the same date as it. Since I ordered the post type by date of that single.post
    – Guruh
    Commented Dec 21, 2022 at 15:44
  • Ah OK: then you probably want to hook when the post is updated and put this code there, rather than as an init hook in single.php. See What is correct way to hook when update post? and How to change the post author when the post is published?
    – Rup
    Commented Dec 21, 2022 at 15:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.