WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Changing Post Date From A Plugin?

[Resolved] Changing Post Date From A Plugin?

Viewing 13 replies - 1 through 13 (of 13 total)
  • 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.

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

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

    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.

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

    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.

    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.

    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.

    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.

    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.

    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

    @getzinger

    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!

    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 ‘[Resolved] Changing Post Date From A Plugin?’ is closed to new replies.
Skip to toolbar