$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Automated mark posts as featured every day|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Automated mark posts as featured every day

matteradmin8PV0评论

Hello Wordpress Community,

I want to build myself a "recipe of the day" functionality.

I'm looking for a way to mark 3 Custom Post Types per day as "Featured Posts" by a plugin or Cronjob.

The next day 3 new posts will be marked as "Featured".

Is there an easy way to do that, or is there a quick and easy way to do this with functions.php in the Child Theme?

Many thanks already

Hello Wordpress Community,

I want to build myself a "recipe of the day" functionality.

I'm looking for a way to mark 3 Custom Post Types per day as "Featured Posts" by a plugin or Cronjob.

The next day 3 new posts will be marked as "Featured".

Is there an easy way to do that, or is there a quick and easy way to do this with functions.php in the Child Theme?

Many thanks already

Share Improve this question asked Jan 14, 2019 at 15:28 FarghoFargho 1176 bronze badges 2
  • Do you want to use native “featured” posts for that? – Krzysiek Dróżdż Commented Jan 14, 2019 at 15:42
  • 1 yes, that would be the easiest way, i guess. – Fargho Commented Jan 14, 2019 at 15:43
Add a comment  | 

1 Answer 1

Reset to default 1

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' );
Post a comment

comment list (0)

  1. No comments so far