Title: Changing Post Date From A Plugin?
Last modified: August 18, 2016

---

# Changing Post Date From A Plugin?

 *  Resolved [golddave](https://wordpress.org/support/users/golddave/)
 * (@golddave)
 * [19 years, 9 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/)
 * Does anyone know how I would go about changing the date (post_date field in the
   wp_posts table) from a plugin?

Viewing 13 replies - 1 through 13 (of 13 total)

 *  [Austin Matzko](https://wordpress.org/support/users/filosofo/)
 * (@filosofo)
 * [19 years, 9 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-451979)
 * `global $wpdb;
    $whatever_time = '2006-08-11 10:56:51'; $whatever_post_id = '
   42'; $wpdb->query("UPDATE $wpdb->posts SET post_date = '$whatever_time' WHERE
   ID = '$whatever_post_id'");
 * will change the post of ID #42 to time 2006-08-11 10:56:51.
 *  Thread Starter [golddave](https://wordpress.org/support/users/golddave/)
 * (@golddave)
 * [19 years, 9 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452006)
 * That’s a great solution. But one more thing. How do I grab the post id?
 *  [Austin Matzko](https://wordpress.org/support/users/filosofo/)
 * (@filosofo)
 * [19 years, 9 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452046)
 * Getting the post id depends on the context of what you’re trying to do.
 *  Thread Starter [golddave](https://wordpress.org/support/users/golddave/)
 * (@golddave)
 * [19 years, 9 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452091)
 * I’m trying to get the post id so that I can change the post_date. Essentially,
   I’m looking to implement the code you gave above but not hard code the post_id.
 *  [Austin Matzko](https://wordpress.org/support/users/filosofo/)
 * (@filosofo)
 * [19 years, 9 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452125)
 * So, what’s the context of what you’re trying to do?
 *  Thread Starter [golddave](https://wordpress.org/support/users/golddave/)
 * (@golddave)
 * [19 years, 9 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452126)
 * Let me try that again since my last reply was silly.
 * This is part of a plugin. The plugin is called by putting a call to the function
   in a page or post as follows:
    < ?php if(function_exists(MyFunction)) MyFunction();?
   > I’d like to change post_date for the post or page that is calling the function.
   Right now I’m using the code suggested by filosofo with a hard coded post ID 
   9and it works). But I want to be able to capture the ID without having it hard
   coded.
 *  [Austin Matzko](https://wordpress.org/support/users/filosofo/)
 * (@filosofo)
 * [19 years, 9 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452132)
 * Change `global $wpdb;`
    to `global $id, $wpdb;`
 * Then if you call your function within the “Loop,” $id will be set to the current
   post’s id.
 *  Thread Starter [golddave](https://wordpress.org/support/users/golddave/)
 * (@golddave)
 * [19 years, 9 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452165)
 * Great!! That worked! Thanks.
 * Is there any way to work the same kind of magic from an Admin page? If you change
   an option from the admin page I’d like it to change the post_date for any post(
   or page) that calls the plugin. Is there a way to do this? Maybe a quick way 
   to search the contents of every post whet the plugin function is called within
   post_content and then update post_date for each of them? Or maybe another method?
 * I know it’s asking much but I have to ask.
 *  [Austin Matzko](https://wordpress.org/support/users/filosofo/)
 * (@filosofo)
 * [19 years, 9 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452216)
 * It would depend on where in the admin panel and what you’re doing.
 * For example, on a write page, I think $post_ID will give the post’s id.
 *  Thread Starter [golddave](https://wordpress.org/support/users/golddave/)
 * (@golddave)
 * [19 years, 9 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452220)
 * It’s an options page. You click Options and then select the options page from
   the list of links along the top of the page.
 *  Thread Starter [golddave](https://wordpress.org/support/users/golddave/)
 * (@golddave)
 * [19 years, 8 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452260)
 * I ended up figuring this one out on my own. Here’s the code I used:
 * global $wpdb;
    $result = $wpdb->get_row(“SELECT * FROM $wpdb->posts WHERE post_content
   = ‘< ?php if(function_exists(MyFunction)) MyFunction(); ?>'”, ARRAY_A); $new_time
   = date(“Y-m-d H:i:s”); $pageid = $result[‘ID’]; $wpdb->query(“UPDATE $wpdb->posts
   SET post_date = ‘$new_time’ WHERE ID = ‘$pageid'”);
 *  [getzinger](https://wordpress.org/support/users/getzinger/)
 * (@getzinger)
 * [19 years, 6 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452342)
 * I need to be able to change Post dates: did you ever finish your plugin to do
   this? Does anyone know of a plugin to update Post dates (timestamps) — or how
   to do it with PHPMyAdmin? Thanks!
 *  Thread Starter [golddave](https://wordpress.org/support/users/golddave/)
 * (@golddave)
 * [19 years, 5 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452346)
 * getzinger –
 * Sorry it’s taken me so long to reply to your message. I added the following code
   to my plugin to change the post date:
 * global $id, $wpdb;
    $new_time = date(“Y-m-d H:i:s”); $wpdb->query(“UPDATE $wpdb-
   >posts SET post_date = ‘$new_time’ WHERE ID = ‘$id'”);
 * (Note that my plugin was not made to manipulate dates so it won’t help you. The
   code I posted will do this within PHP so you can use it in a new plugin if you
   wish.)

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘Changing Post Date From A Plugin?’ is closed to new replies.

## Tags

 * [date](https://wordpress.org/support/topic-tag/date/)
 * [post_date](https://wordpress.org/support/topic-tag/post_date/)
 * [wp_posts](https://wordpress.org/support/topic-tag/wp_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 13 replies
 * 3 participants
 * Last reply from: [golddave](https://wordpress.org/support/users/golddave/)
 * Last activity: [19 years, 5 months ago](https://wordpress.org/support/topic/changing-post-date-from-a-plugin/#post-452346)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
