Support » Fixing WordPress » problem with wp_delete_post

  • Hi

    I’m putting together a competition agregation site and having problems with wp_delete_post. THe issue is that it is sending newly published posts directly to trash.

    When people add new competitions normally there is no problems with publishing but I wanted the posts to auto-delete once it reaches the closing date. This is the code I use in the single.php file – it is in the loop:

    $expirationtime = get_post_custom_values('imwb_socialpress_post_closing_date');
    if (is_array($expirationtime)) {
    $expirestring = implode($expirationtime);
    $secondsbetween = strtotime($expirestring)-time();
    if ( $secondsbetween >= -10) { blah balh blah, building the post.
    } else { echo 'Competition closed';
    if ( $secondsbetween < -11) { 
    wp_delete_post(get_the_ID()); }

    The way it should work is that when the closing date arrives the first visitor to the page will get a competition is closed notification and the visit should fire off the delete post action.

    This does work as wanted except that it sends any newly created posts to the trash also.

    If I delete the

    if ( $secondsbetween < -11) { 
    wp_delete_post(get_the_ID()); }

    Then the posts will publish as expected and when the closing date is reached then the compeition is closed notification appears but obviously the post is not then moved to trash.

    I set the $secondsbetween conditional to -11 because I thought there may have been an issue with that but is has no impact on allowing publishing when wp_delete_post is included.

    Am I doing something wrong that particularly stands out.
    The custom field that I am using for people to store the closing date uses the YYYY-MM-DD format which should be ompatible with the php strtotime function. I am running php 5.3.


Viewing 4 replies - 1 through 4 (of 4 total)
  • vtxyzzy


    Try echoing the value of $expirestring instead of issuing the wp_delete_post. It may not have the value you expect.


    thanks for the suggestion.

    You could be on the right track there 🙂

    Looking at the code I don’t need the $expirestring as it is returnning the same value as $expirationtime because the custom value is not an array.

    The $expirestring is returning the closing date in the format of eg 2013-11-27

    I might try getting rid of that code altogether and using the $expirationtime variable directly.




    I think the problem might be that you have no hours-minutes-seconds on your $expirationtime. That means that strtotime will assume zeros (I think), and anything on the same day will give a large number of seconds difference.

    Try adding the seconds to $expirationtime like this:

    $expirationtime .= ' 23:59:59';


    Thanks I think you have me going in the right direction.

    Rather than add the hours to the expirationtime varible I changd the php option from time to date.

    That seems to be working by allowing posting. I’ll have ot check it sends outdated comps to trash though whwn one runs out.

    If it doesn’t work you nudges has led me to learn a bit more about the timestamp options of php and I’l look at using the datetime diff functions which I’ve used to add a date countdown to each page.

    That could also be an option for firing off the wp delete posts when the date difference is equal to 0.

    thanks again

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘problem with wp_delete_post’ is closed to new replies.