• Hi.

    For our category, currently at the movies (film-nu-in-de-zaal), we would like to implement a function that after 6 weeks this changes to the category movie reviews (film-recensies). (For testing purpouses the time is set to 11 minutes, since anything lower than this won’t execute the update function.)

    Using an example on stackoverflow and the codex I managed to get this far, 3/4th of the code works. The code fires at the set time and the category is removed.

    Sadly… the category is set to “uncategorized” every single time. We have tried implementing the numbers, ditching the old and new category variables but nothing seems to do the trick.

    Can anyone help us out here ?

    Thanks in advance

    // runs when a post is published
    add_action( 'publish_post', 'schedule_post_check' );
    
    function schedule_post_check( $post_id ) {
    
    // Schedule the event
    wp_schedule_single_event( time() + 660, 'check_post', array( $post_id ) ); 
    }
     
    
    // a custom hook to schedule
    add_action( 'check_post', 'check_post_cats', 10, 1 );
    
    // replace post categories
    function check_post_cats( $post_id ) {
    
        //categories
        $old_cat = get_cat_ID( 'film-nu-in-de-zaal' );
        $new_cat = get_cat_ID( 'film-recensies' );
    
        // check for the old category
        if ( has_category( $old_cat, $post_id ) ) {
            // update post with new category
            $args = array(
                'ID'            => $post_id,
                'post_category' => array( $new_cat ),
            );
            wp_update_post($args);
        }
    }
    • This topic was modified 6 years, 1 month ago by nessler.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    get_cat_ID returns a 0 if the function fails. What is the value of $new_cat?

    You will not be able to use wp_update_post() to set the post terms. Try using wp_set_post_terms() instead with the $append parameter set to false:

    wp_set_post_terms( $post_id, array( $new_cat ), 'category', false );

    Thread Starter nessler

    (@nessler)

    $old_cat = get_cat_ID( ‘film-nu-in-de-zaal’ ); should return 39
    $new_cat = get_cat_ID( ‘film-recensies’ ); should return 12

    I’ll try the set post terms later when I get home and let you know if that worked.

    Thread Starter nessler

    (@nessler)

    adjusted the code as you suggested above, but still no category is added.
    Something changed though, instead of “uncategorized” there is now a dash “—”.

    Full code as I have it now:

    // runs when a post is published
    add_action( 'publish_post', 'schedule_post_check' );
    
    function schedule_post_check( $post_id ) {
    
    // Schedule the event
    wp_schedule_single_event( time() + 660, 'check_post', array( $post_id ) ); 
    }
     
    
    // a custom hook to schedule
    add_action( 'check_post', 'check_post_cats', 10, 1 );
    
    // replace post categories
    function check_post_cats( $post_id ) {
    
        //categories
        $old_cat = get_cat_ID( 'film-nu-in-de-zaal' );
        $new_cat = get_cat_ID( 'film-recensies' );
    
        // check for the old category
        if ( has_category( $old_cat, $post_id ) ) {
          wp_set_post_terms( $post_id, array( $new_cat ), 'category', false );
        }
    }
    
    ?>

    Maybe instead of passing an array of terms to the wp_set_post_terms() function you just pass $new_cat.

    You should also be checking if the event is already scheduled before scheduling it again using wp_next_scheduled(). See the examples on wp_schedule_event().

    Thread Starter nessler

    (@nessler)

    sadly no success either.

    I came up with another solution which does work, though I still need to make it category specific so that it only fires on the “film nu in de zaal” category, as it is now all articles will be moved.

    // runs when a post is published
    add_action( 'publish_post', 'schedule_post_check' );
    
    function schedule_post_check( $post_id ) {
    
    // Schedule the event
    wp_schedule_single_event( time() + 660, 'check_post', array( $post_id ) ); 
    } 
    
    // a custom hook to schedule
    add_action( 'check_post', 'check_post_cats', 10, 1 );
    
    // replace post categories
    function check_post_cats( $post_id ) {
    
        // An array of IDs of categories we want this post to have.
    $cat_ids = array( 12 );
    
    $term_taxonomy_ids = wp_set_object_terms( $post_id, $cat_ids, 'category' );
    
    if ( is_wp_error( $term_taxonomy_ids ) ) {
    	// There was an error somewhere and the terms couldn't be set.
    } else {
    	// Success! The post's categories were set.
    }        
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Category change after set time’ is closed to new replies.