WordPress: Funktion som drafter indlæg efter 2 måneder
Jeg har lavet denne lille funktion som drafter alle indlæg som er ældre end 2 måneder. Funktionen skal indsættes i dit temas functions.php fil og kan fx køres via et wp-cron eller manuelt via en get funktion.
// Helper function
function change_post_status($post_id,$status){
$current_post = get_post( $post_id, 'ARRAY_A');
$current_post['post_status'] = $status;
wp_update_post($current_post);
}
// Update function
function putoldpostsasdraft() {
$posts = get_posts(array('post_type' => 'post', 'posts_per_page' => - 1, 'date_query' => array(array( 'column' => 'post_date_gmt', 'before' => '2 month ago')) ) );
foreach ($posts as $post) {
$id = $post->ID;
change_post_status($id, 'draft');
var_dump('drafted: '. $id);
}
}
WordPress indlægsstatusser:
Ønsker du at ændre indlæggets status til andet end kladde så har jeg har listet en række WordPress indlægsstatusser som kan benyttes, udover “draft”:
publish|pending|draft|private|static|object|attachment|inherit|future|trash