posts - ACF date picker to trigger category change

admin2025-01-08  4

I have a ACF date picker that currently changes the post status from “Publish” to “Draft. Yet I am also trying to change the post category. If anyone can point me in the right direction, that would be most appreciated.

    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); 
        }
    }
}

I have a ACF date picker that currently changes the post status from “Publish” to “Draft. Yet I am also trying to change the post category. If anyone can point me in the right direction, that would be most appreciated.

    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); 
        }
    }
}

Share Improve this question edited Nov 3, 2021 at 15:11 Tom J Nowell 60.7k7 gold badges77 silver badges147 bronze badges asked Nov 3, 2021 at 14:16 jontronicsjontronics 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Looks like I found the issue. In order to change the category of a post , you cannot use wp_udpate_post() to do so. Instead I used wp_set_post_categories(); Also wanted to note that these are categories that I created in the meta box that is displayed in the post edit page.

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); 
        }
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267177a1178.html

最新回复(0)