0

I'm hoping to write a cron to automatically change the custom post type for any items that match a particular CPT each night at midnight.

For instance: I have a CPT called "members," but at midnight or so each day, I am hoping to flip any items with that CPT to just a regular "posts" post type. I've searched high and low and haven't been able to figure this out.

Any help is appreciated.

1 Answer 1

0

Try something like this:

if (!wp_next_scheduled('update_members_types')) {
    wp_schedule_event( time(), 'daily', 'update_members_types' );
}
add_action ( 'update_members_types', 'update_member_post_type' );

function update_member_post_type() {

    $args = array(
        'post_type' => 'member',
        'posts_per_page' => '1',
        'date_query' => array(
            array(
                'after' => strtotime( '-24 hours' )
            )
        )
    );
    $members = get_posts( $args );

    if ( $members ) {
        foreach ( $member as $members ) {
            set_post_type( $member->ID, 'post' );
        }
    }

} 

Wordpress has a function which allows you to update the post type for the post ID.

https://developer.wordpress.org/reference/functions/set_post_type/

Note that the cron task is not scheduled to run per say at midnight each day but every 24 hours from the time of initialisation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.