Support

Account

Home Forums Backend Issues (wp-admin) Use radio buttons to assign custom taxonomy slug to custom post type

Solved

Use radio buttons to assign custom taxonomy slug to custom post type

  • Hey guys,

    I have custom post type called ‘asc_video’ that have once youtube video embedded per post.
    The user should be able to set a featured video using radio buttons (Yes or No, where No is the default value). Only one video can be the featured video, because it is going to be shown on the archives page.
    To achieve that, I have created a custom taxonomy called ‘featured_video’ and assigned it to the custom post type. The taxonomy only has one slug in it, called ‘yes’.

    My thought process is checking wether or not the user has selected the radio button ‘Yes’, if he did, get all posts that have the slug ‘yes’ attached to them (which is only one post of course) and then updating the value of the radio button to ‘No’ and removing the slug ‘yes’ from that video.

    After that, I simply want to assign the ‘yes’ slug to the post that is beeing saved.

    Everything works fine, apart from the fact that the post that is beeing saved, is not getting the ‘yes’ slug assigned to it.

    Here is the code from my functions.php:

    function my_acf_save_post($post_id) {
    
    	// get the value of the radio button that is selected
    	$value = get_field('is_featured_video');
    
    	// only run the code, if the user has selected "Yes" (yes_featured_video)
    	if($value == 'yes_featured_video') {
    		// start by fetching the terms for the featured_video taxonomy (there is only one, which is 'yes')
    		$terms = get_terms( 'featured_video' );
    
    		if(!empty($terms)) {
    			// now run a query for each term
        		foreach ( $terms as $term ) {
    	        	$args = array(
    		        'post_type' => 'asc_video',
    		        'featured_video' => $term->slug
    		    );
    		    $query = new WP_Query( $args );
    		    }
    
    			// start the loop
    		 	while ( $query->have_posts() ) : $query->the_post();
    		 		// remove the slug from the video that previously had the 'yes' slug assigned to it
    				wp_set_object_terms( get_the_ID(), '', 'featured_video');
    				// set the radio button value to "No" (no_featured video)
    				update_field('is_featured_video','no_featured_video');
    		 	endwhile; 
    
    		}
    
    		// assign the 'yes' slug to the post
    		wp_set_object_terms($post_id, 'yes', 'featured_video');
    		
    	}
    
       
    
    }
    
    add_action('acf/save_post', 'my_acf_save_post');

    I really have no clue on where im going wrong. Do you guys have any suggestions?

    Thank you very much 🙂

  • change your query to not include the current post

    
    $args = array(
      'post_type' => 'asc_video',
      'featured_video' => $term->slug,
      'post__not_in' => array($post_id);
    );
    $query = new WP_Query( $args );
    

    https://codex.wordpress.org/Class_Reference/WP_Query

  • Thanks for the reply.

    Unfortunately, this did not solve the problem. The post that is beeing saved is still not getting the custom taxonomy attached to it.

    I assume, that the current post is not even in the query, because it doesn’t have the ‘yes’ slug at the time of the query.

  • I was wrong, in your loop you need to check if it’s the current post, if it is you set the term and if it’s not you clear the term, like you’re doing. Since you’re using a radio field it does not get set automatically. You also need to declare the global $post, and you need to do wp_reset_postdata after your loop. something like this.

    
    <?php 
    
      function my_acf_save_post($post_id) {
        global $post;
        // get the value of the radio button that is selected
        $value = get_field('is_featured_video');
      
        // only run the code, if the user has selected "Yes" (yes_featured_video)
        if($value == 'yes_featured_video') {
          // start by fetching the terms for the featured_video taxonomy (there is only one, which is 'yes')
          $terms = get_terms( 'featured_video' );
      
          if(!empty($terms)) {
            // now run a query for each term
              foreach ( $terms as $term ) {
                  $args = array(
                  'post_type' => 'asc_video',
                  'featured_video' => $term->slug
              );
              $query = new WP_Query( $args );
              }
      
            // start the loop
            while ( $query->have_posts() ) : $query->the_post();
              // remove the slug from the video that previously had the 'yes' slug assigned to it
              if ($post_id != $post->ID) {
                wp_set_object_terms( get_the_ID(), '', 'featured_video');
              // set the radio button value to "No" (no_featured video)
                update_field('is_featured_video', 'no_featured_video');
              } else {
                wp_set_object_terms($post_id, $term, $term->taxonomy);
              }
            endwhile; 
            wp_reset_postdata();
          }
      
          // assign the 'yes' slug to the post
          wp_set_object_terms($post_id, 'yes', 'featured_video');
          
        }
      
         
      
      }
      
      add_action('acf/save_post', 'my_acf_save_post');
    
    ?>
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.