Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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?

share|improve this question

migrated from stackoverflow.com Apr 20 '12 at 14:09

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers 2

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.'.';
share|improve this answer
    
You'll need to set the parameters of get_the_date() to use the correct format for your math. –  developdaly Apr 20 '12 at 14:40
    
the math is a bit off. the date will always be higher than the total seconds. I'm on my phone, but something like this to do the check. $now=time(); $date=get_the_date(); if (($date + 172800) >= $now) then delete it. –  Norcross Apr 21 '12 at 10:49
add comment

I'm not sure of what you're trying to do but why don't you select the post_date in your request directly ?

SELECT post_date FROM wp_posts WHERE ...

You should then be able to test it in PHP (see this answer) and do what you want!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.