Code for dynamically adding taxonomy term to posts isn't applying the term

Alicia St Rose
Alicia St Rose used Ask the Experts™
I had this working for custom post type called episode. But had to switch to posts.

I no longer have the term id of 173 which is why it's commented out.

Right now I'm not getting the term premium swapped out for Free once I hit the 11th post. It still says Free for the last 11 posts and if I add a new one it just keeps the Free term and not Premium.

/*Add terms to chapters */

add_action( 'save_post_episode', 'total_set_default_object_terms', 11, 2 );
function total_set_default_object_terms( $post_id, $post ) {
  if ( 'publish' === $post->post_status) {
    $position = 1;
    $args = [
      'numberposts' => -1,
      'order' 			=> 'DESC',
      'orderby' 		=> 'date',
      'post_type' 	=> 'post',
      'taxonomy' => 'episode_type',
      //'tax_query' => array(
					//'taxonomy' => 'episode_type',
					//'field' => 'id',
					//'terms' => array( '173' ),
					//'operator' => 'NOT IN',
    $posts = get_posts( $args );// query posts that are episodes in descending publish date order
    foreach($posts as $post) {
      if ($position > 10) {
        wp_set_object_terms($post->ID, 'premium', 'episode_type', false);
      $taxonomies = get_object_taxonomies( $post->post_type );

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Owner & Principle Developer/Designer
Figured it out:

Needed to change

add_action( 'save_post_episode', 'total_set_default_object_terms', 11, 2 );

Open in new window


add_action( 'save_post', 'total_set_default_object_terms', 11, 2 );

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial