Example WP-cronjob

function schedule_post_check( $post_id ) {
	// Schedule the event
	wp_schedule_single_event( time() + 16, 'check_post', array( $post_id ) ); 
}
// runs when a post is published
add_action( 'publish_post', 'schedule_post_check' ); 



// replace post categories
function check_post_cats( $post_id ) {

	$old_cat = get_category_by_slug('actueel'); 
	$old_id = $old_cat->term_id;

	$new_cat = get_category_by_slug('archief'); 
	$new_id = $new_cat->term_id;
	$time = current_time('mysql');

   // check for the old category
   if ( has_category( $old_id, $post_id ) ) {
		wp_set_post_terms( $post_id, array( $new_id ), 'category', false );

	    if ( ! wp_is_post_revision( $post_id ) ){
	     
	        // unhook this function so it doesn't loop infinitely
	        remove_action('publish_post', 'schedule_post_check');

			wp_update_post(
				array (
					'ID'            => $post_id, // ID of the post to update
					//'post_modified' => $time,
					//'post_modified_gmt' => get_gmt_from_date( $time )
					'post_date'     => $time,
					'post_date_gmt' => get_gmt_from_date( $time )
				)
			);

	        // re-hook this function
	        add_action( 'publish_post', 'schedule_post_check' ); 
	    }
   }
}
// a custom hook to schedule
add_action( 'check_post', 'check_post_cats', 10, 1 );

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

© 2020 Jakob Helmer - Powered by WordPress - @trails&tribulations