Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to auto draft a specific posts_type after a period of time? WordPress

  • SOLVED

Hi!

I would like to have a specific post_type like for example 'movies'
put in to draft mode in let's say like after a period of 30 days.

When it get's re-published it needs to be active 30 days again and then
after that period put into draft mode again and so on, until it's deleted by the user.

So all posts of this post_type have to do this.

Could you help me out with a plugin or functions.php code?

Thanks!

Kind regards,

Jaap

Answers (2)

2011-09-22

Abdessamad Idrissi answers:

I'm working on a solution for you and without any plugin :)


jaap comments:

Wow, thank you very much, I'm eagerly waiting!


Abdessamad Idrissi comments:

Here you are:

/**
* post_type like for example 'movies' put in to draft mode
* in let's say like after a period of 30 days..
*/
if (!wp_next_scheduled('draft_movies_hook')) {
wp_schedule_event( time(), 'daily', 'draft_movies_hook' );
}

/**
* Tell WordPress to run do_draft_movies() when the 'draft_movies_hook' hook is run.
*/
add_action( 'draft_movies_hook', 'do_draft_movies' );

/**
* Sets the post excerpt length to 40 words.
*
* To override this length in a child theme, remove the filter and add your own
* function tied to the excerpt_length filter hook.
*/
function do_draft_movies() {

$args = array(
'post_type' => 'movies' //change this to your post type
);

$query = new WP_Query($args);
if( $query ->have_posts() ) {
while ($query->have_posts()) : $query->the_post();
$last_edit = get_the_modified_time( 'U' );
$next_edit = $last_edit + (30 * 24 * 60 * 60);

if( time() > $next_edit):
$draft_post = array();
$draft_post['ID'] = get_the_ID();
$draft_post['post_status'] = 'draft';

// Update the post into the database
wp_update_post( $draft_post );
endif;

endwhile;

}

wp_reset_query();
}


have a nice time :)


Abdessamad Idrissi comments:

Ooops! I forget to change some function comments there :)
here's it again..

/**
* post_type like for example 'movies' put in to draft mode
* in let's say like after a period of 30 days..
*/
if (!wp_next_scheduled('draft_movies_hook')) {
wp_schedule_event( time(), 'daily', 'draft_movies_hook' );
}

/**
* Tell WordPress to run do_draft_movies() when
* the 'draft_movies_hook' hook is run.
*/
add_action( 'draft_movies_hook', 'do_draft_movies' );

/**
* Updates the post_status to draft
*
* Only if the post lst time edit is
* more than one month.
*/
function do_draft_movies()
{

$args = array(
'post_type' => 'movies' //change this to your post type
);

$query = new WP_Query($args);
if( $query ->have_posts() ) {
while ($query->have_posts()) : $query->the_post();
$last_edit = get_the_modified_time( 'U' );
$next_edit = $last_edit + (30 * 24 * 60 * 60);

if( time() > $next_edit):
$draft_post = array();
$draft_post['ID'] = get_the_ID();
$draft_post['post_status'] = 'draft';

// Update the post into the database
wp_update_post( $draft_post );
endif;

endwhile;

}

wp_reset_query();
}


jaap comments:

Wow thanks, thats fast, I will take a look tomorrow looks great!


jaap comments:

Hi there!

I've pasted the code into my functions.php file.

I would like to test it so I have changed the following code:
$next_edit = $last_edit + (30 * 24 * 60 * 60);


to 1 day:
$next_edit = $last_edit + (1 * 24 * 60 * 60);

and the daily:
wp_schedule_event( time(), 'daily', 'draft_movies_hook' );

to hourly:
wp_schedule_event( time(), 'hourly', 'draft_movies_hook' );

Is there any way to test this quicker?

Thanks in advance!


Abdessamad Idrissi comments:

The wp_schedule_event requires a minimum of at least 1 hour to execute. and since this event was already registred in the databse, you will need to clean it and create a new one
wp_clear_scheduled_hook('draft_movies_hook');
but if you want just to test if the function works, you can call it by:
do_draft_movies();
and to make it faster change:
$next_edit = $last_edit + (30 * 24 * 60 * 60);
to
$next_edit = $last_edit + (60);
so that this function executes every minute!


jaap comments:

Thanks for your help, here's the full code for ever who's interested:

<?php

/**
* post_type like for example 'movies' put in to draft mode
* in let's say like after a period of 30 days..
*/
if (!wp_next_scheduled('draft_movies_hook')) {
wp_schedule_event( time(), 'daily', 'draft_movies_hook' );
}
wp_clear_scheduled_hook('draft_movies_hook');

/**
* Tell WordPress to run do_draft_movies() when
* the 'draft_movies_hook' hook is run.
*/
add_action( 'draft_movies_hook', 'do_draft_movies' );

/**
* Updates the post_status to draft
*
* Only if the post lst time edit is
* more than one month.
*/
function do_draft_movies()
{
$args = array(
'post_type' => 'vacatures' //change this to your post type
);

$query = new WP_Query($args);
if( $query ->have_posts() ) {
while ($query->have_posts()) : $query->the_post();
$last_edit = get_the_modified_time( 'U' );
$next_edit = $last_edit + (30 * 24 * 60 * 60);
//$next_edit = $last_edit + (60);
if( time() > $next_edit ):
$draft_post = array();
$draft_post['ID'] = get_the_ID();
$draft_post['post_status'] = 'draft';

// Update the post into the database
$ID = wp_update_post( $draft_post );

//echo 'updated post with id '.$ID.' | now > $next_edit : '.time().'>'. $next_edit;
else:
//echo 'nothing updated because this is not true; now > $next_edit : '.time().'>'. $next_edit;
endif;

endwhile;

} else {
//echo 'no posts found of type: '.$args['post_type'];
}

wp_reset_query();
}
do_draft_movies();

function clean_ghost_vacatures()
{
$args = array( //change this to your post type
);

$query = new WP_Query($args);
if( $query ->have_posts() ) {
while ($query->have_posts()) : $query->the_post();
$draft_post = array();
$draft_post['ID'] = get_the_ID();
$draft_post['post_status'] = 'draft';

// Update the post into the database
$ID = wp_update_post( $draft_post );

echo 'fixed ghost vacature with id '.$ID.' \n \nl ';
endwhile;

} else {
echo 'no ghost vacatures found! \n \nl';
}

wp_reset_query();
}
//clean_ghost_vacatures();
?>

2011-09-22

Mariano Pereyra answers:

This plugin:

http://wordpress.org/extend/plugins/post-expirator/

could be used as a base for what you need.


Mariano Pereyra comments:

There seems to be a whole category dedicated to this kind of plugins:

http://wordpress.org/extend/plugins/tags/expire


jaap comments:

Thanks you for your reply but I have already tested those plugins.
And none are specificly doing what I would like.