Ask your WordPress questions! Pay money and get answers fast! (more info)

function to expire post by author id after 7 days from publish WordPress

  • SOLVED

Hello,

Can anyone please help me with a function to change posts from Published, to Trashed, 7 days from when the post was published.

But only for posts that are created by a specific order. So this could be defined by the author id or the author slug.


I found this function below which is supposed to do it upon specific category ID's, but I cant seem to get it to work.


First person with a working function will be get the full amount.



For demo purposes, can I have an option where the time variable is set for 30secs? This is so I can test. And then the real variable for 7 Days.



This code below may help someone get started.


function expire_posts() {
global $wpdb;
$daystogo = "15";
$protcats = array( 9,10 );
$excludeCats = '';
foreach($protcats as $cat){
$excludeCats .= " AND `post_category` != '$cat' "; //add each category to it's own exclude
}
$sql = "UPDATE wp_posts SET `post_status` = 'draft' WHERE `post_type` = 'post' ". $excludeCats ." AND DATEDIFF(NOW(), `post_date`) > '$daystogo')";
}
add_action('wp_head', 'expire_posts');




Many Thanks

Answers (1)

2012-10-29

Dbranes answers:

Hi, here is one idea using wp-cron

(copy this into your functions.php and remember to take backup before )

The time-interval is now 'hourly'



// clear my_trash_hook from wp-cron
//wp_clear_scheduled_hook('my_trash_hook');

// run the function "my_trash_hook_function" with wp-cron
add_action( 'my_trash_hook', 'my_trash_hook_function' );
if (!wp_next_scheduled('my_trash_hook')) {
wp_schedule_event( time(), 'hourly', 'my_trash_hook' );
}

// the function we want to run
function my_trash_hook_function() {

global $wpdb,$post;

$daystogo = "7"; // trash post older than daysago
$user_id="1"; // trash post from user with user_id

$sql = $wpdb->prepare("UPDATE wp_posts SET post_status = 'trash' WHERE post_status='publish' AND post_type = 'post' AND post_author='%s' AND DATEDIFF(NOW(), post_date) > %d",$user_id,$daystogo);

$wpdb->query($sql);
}



Josh Cranwell comments:

Thanks dude will test now :-)


Dbranes comments:


Here is a plugin, so you can see all the running cron-jobs in your WP:

http://wordpress.org/extend/plugins/cron-view/


Josh Cranwell comments:

Thanks, so when this function is running hourly, does it run on the hour...

17:00
18:00
19:00
20:00
etc

?

I have backdated my test post by a week to test it whether it jumps to trash on the next coming hour.

So we will see, I may get back to you tomorrow if thats OK, as I will be traveling. Thanks


Dbranes comments:

ok let's see what happens ;-)

The time interval starts when you first activate the code.

So when you view the cron schedule (with the cron-view plugin) you get a row like this:

Oct 29, 2012 @ 17:19 (1351531143) Once Hourly my_trash_hook


where the next run is around 17:19.

ps:
I assume that your post table is named wp_posts.

pss:

If you want to test the update function on a single post you can use this:

// Test
add_action("init","test_my_trash_hook_function");
function test_my_trash_hook_function() {
global $wpdb,$post;

$post_id="9"; // trash post with post_id
$daystogo = "7"; // trash post older than daysago
$user_id="1"; // trash post from user with user_id

$sql = $wpdb->prepare("UPDATE wp_posts SET post_status = 'trash' WHERE post_status='publish' AND post_type = 'post' AND post_author='%s' AND ID=%d AND DATEDIFF(NOW(), post_date) > %d",$user_id,$post_id,$daystogo);

$wpdb->query($sql);

}


Josh Cranwell comments:

Ahhh that croc plugin is very clever.

My next schedule is in 48mins.

So hopefully yes it jump to trash then, yeah I will let you know.


Also yes wp_posts is defined here in my config? And yes my test site has this...

$table_prefix = 'wp_';

But if prefix is different, do I need to change... $wpdb->prepare("UPDATE wp_posts


Thanks


Dbranes comments:

yes you change the prefix there.

You can also change

$wpdb->prepare("UPDATE wp_posts SET


into

$sql = $wpdb->prepare("UPDATE $wpdb->posts SET


so you don't have to worry about the table name ;-)


Josh Cranwell comments:

Hi Dbranes,

I tested the function on two systems, and using the auto table prefix variable.

However the post's seem to continue as published. Here is the code as I used...

// run the function "my_trash_hook_function" with wp-cron
add_action( 'my_trash_hook', 'my_trash_hook_function' );
if (!wp_next_scheduled('my_trash_hook')) {
wp_schedule_event( time(), 'hourly', 'my_trash_hook' );
}

// the function we want to run
function my_trash_hook_function() {

global $wpdb,$post;
$daystogo = "7"; // trash post older than daysago

$user_id="5"; // trash post from user with user_id
$sql = $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'trash' WHERE post_status='publish' AND post_type = 'post' AND post_author='%s' AND DATEDIFF(NOW(), post_date) > %d",$user_id,$daystogo);
$wpdb->query($sql);

}


I used the cron plugin (very cool) to monitor the <strong>my_trash_hook</strong> but it the post continues to run.

I triple checked my test dates and user ID's - and theoretically the post by user_id=5 should have moved to trash, but it did not work for me.

Does it work for you?


Many Thanks
Josh


Josh Cranwell comments:

Actually, ignore that...

Got it to working!

Thank you very much for your help.

Will contact you direct if I need to extend the function ever.


Many Thanks