Automated mark posts as featured every day

OK, so first of all you’ll need to add your own WP_Schedule event:

add_action( 'wp', function () {
    if (! wp_next_scheduled ( 'mark_posts_as_featured_event' )) {
        wp_schedule_event(time(), 'daily', 'mark_posts_as_featured_event');
    }
} );

function mark_posts_as_featured_event_callback() {
    // if there are sticky posts in our CPT, unstick them
    $sticked_post_ids = get_option( 'sticky_posts' );
    if ( ! empty ) { 
        $old_featured_posts = get_posts( array(
            'post_type' => '<MY_POST_TYPE>',
            'fields' => 'ids',
            'post__in' => $sticked_post_ids,
        ) );

        foreach ( $old_featured_post_ids as $post_id ) {
            unstick_post( $post_id );
        }
    }

    // stick new posts
    // get_random_posts
    $new_featured_post_ids = get_posts( array(
        'post_type' => '<MY_POST_TYPE>',
        'posts_per_page' => 3,
        'orderby' => 'rand',
        'fields' => 'ids',
    ) );

    foreach ( $new_featured_post_ids as $post_id ) {
        stick_post( $post_id );
    } 
}
add_action( 'mark_posts_as_featured_event', 'mark_posts_as_featured_event_callback' );

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)