Remove posts after a given amount of time

guys what I’m trying to do is run a series of if checks in the code before executing a series of functions and custom code. One of these if checks will check to see if a post already exists and if so it will then check how old the post is and if the post is say over 48 hours in age it will delete that post and recreate a new one.

Something to the effect of:

Read More
global $wpdb;

if ( !is_user_logged_in())
   echo 'You must be logged in.';

// check if title $my_post_name already exists in wpdb
else if ( is_user_logged_in()&& $wpdb->get_row("SELECT post_title FROM wp_posts WHERE post_title = '" . $my_post_name . "'", 'ARRAY_A')) {

// then check age of the post? assign age to a variable?
  $time = age-of-post?

if $time >= 172800 // in seconds
code:  delete the post and create a new one 
else
$xtime = 172800 - $time;
echo 'You have already challenged' .$character. 'within the last 48 hours. You can challenge' .$character.'again in' .$xtime.'.';

I hope that sort of make sense. My question is how do you get the age-of-post and is there a function to delete a post already in wordpress? Or is there a simpler way to make posts go POOF after x amount of time?

Related posts

Leave a Reply

2 comments

  1. You can use wp_delete_post() and get_the_date().

    global $wpdb;
    
    if ( !is_user_logged_in())
       echo 'You must be logged in.';
    
    // check if title $my_post_name already exists in wpdb
    else if ( is_user_logged_in()&& $wpdb->get_row("SELECT post_title FROM wp_posts WHERE post_title = '" . $my_post_name . "'", 'ARRAY_A')) {
    
    // then check age of the post? assign age to a variable?
      $time = get_the_date();
    
    if ( $time >= 172800 ) // in seconds
        wp_delete_post()
    else
    $xtime = 172800 - $time;
    echo 'You have already challenged' .$character. 'within the last 48 hours. You can challenge' .$character.'again in' .$xtime.'.';