每天将3个随机帖子分配给一个类别

时间:2019-03-10 10:14:47

标签: wordpress custom-post-type custom-taxonomy

我想创建当天功能的食谱。

因此,我想将3种自定义帖子类型为“ recipe”的wordpress帖子添加到“每日食谱”类别中。

我也想从狂欢节中删除当天的食谱。

我已经有几行代码,但是我不知道如何在其中分配和取消分配类别。

TLDR:我的问题:我不知道如何为循环分配和取消分配类别。

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 ) {
            // unassign category
        }
    }

    // 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 ) {
        // assign category
    } 
}
add_action( 'mark_posts_as_featured_event', 'mark_posts_as_featured_event_callback' );

0 个答案:

没有答案