WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Changing Post Date From A Plugin? (14 posts)

  1. golddave
    Member
    Posted 7 years ago #

    Does anyone know how I would go about changing the date (post_date field in the wp_posts table) from a plugin?

  2. Austin Matzko
    Member
    Posted 7 years ago #

    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.

  3. golddave
    Member
    Posted 7 years ago #

    That's a great solution. But one more thing. How do I grab the post id?

  4. Austin Matzko
    Member
    Posted 7 years ago #

    Getting the post id depends on the context of what you're trying to do.

  5. golddave
    Member
    Posted 7 years ago #

    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.

  6. Austin Matzko
    Member
    Posted 7 years ago #

    So, what's the context of what you're trying to do?

  7. golddave
    Member
    Posted 7 years ago #

    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.

  8. Austin Matzko
    Member
    Posted 7 years ago #

    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.

  9. golddave
    Member
    Posted 7 years ago #

    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.

  10. Austin Matzko
    Member
    Posted 7 years ago #

    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.

  11. golddave
    Member
    Posted 7 years ago #

    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.

  12. golddave
    Member
    Posted 7 years ago #

    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'");

  13. getzinger
    Member
    Posted 7 years ago #

    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!

  14. golddave
    Member
    Posted 7 years ago #

    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.)

Topic Closed

This topic has been closed to new replies.

About this Topic