ACF日期选取器触发类别更改

时间:2021-11-03 作者:jontronics

我有一个ACF日期选择器,目前将帖子状态从“发布”更改为“草稿”。但我也在尝试更改帖子类别。如果有人能为我指出正确的方向,我将不胜感激。

    if ($expireTransient = get_transient($post->ID) === false) {
    set_transient($post->ID, \'set for 1 minutes\', 1 * MINUTE_IN_SECONDS );
    $today = date(\'Y-m-d H:i:s\', current_time(\'timestamp\', 0));
    $args = array(
        \'post_type\' => \'post\',
        \'category_name\' => \'\',
        \'posts_per_page\' => 200,
        \'post_status\' => \'publish\',
        \'meta_query\' => array(
            array(
                \'key\' => \'end_date_time\', // ACF field name 
                \'value\' => $today,
                \'compare\' => \'<=\'
            )
        )
    );

    $posts = get_posts($args);
   
    foreach( $posts as $post ) {
        if(get_field(\'end_date_time\', $post->ID)) {
     
            $postdata = array(
                \'ID\' => $post->ID,
                \'category_name\' => \'premium\', // Change category name slug here?
                \'post_status\' => \'draft\'
            );      

            wp_update_post($postdata); 
        }
    }
}

1 个回复
SO网友:jontronics

看来我找到问题了。要更改帖子的类别,不能使用wp\\u udpate\\u post()进行更改。相反,我使用了wp\\u set\\u post\\u categories();还想指出,这些是我在编辑后页面中显示的元框中创建的类别。

if ($expireTransient = get_transient($post->ID) === false) {
    set_transient($post->ID, \'set for 1 minutes\', 1 * MINUTE_IN_SECONDS );
    $today = date(\'Y-m-d H:i:s\', current_time(\'timestamp\', 0));
    $args = array(
        // \'category_name\' => \'\',
        \'post_type\' => \'post\',
        \'posts_per_page\' => 200,
        \'post_status\' => \'publish\',
        \'meta_query\' => array(
            array(
                \'key\' => \'end_date_time\',
                \'value\' => $today,
                \'compare\' => \'<=\'
            )
        )
    );
    $posts = get_posts($args);
    foreach( $posts as $post ) {
        if(get_field(\'end_date_time\', $post->ID)) {
     
            $postdata = array(
       
                \'ID\' => $post->ID,
                \'post_status\' => \'draft\'
            );

            // Added Code 
            $post_id = $post->ID;

            wp_set_post_categories( $post_id, array(13003), $append ); // id of category in array. 

            wp_update_post($postdata); 
        }
    }
}