Support

Account

Home Forums ACF PRO True/False – Only allow one post to have true value Reply To: True/False – Only allow one post to have true value

  • Hi John,

    Thank you for your reply. I’m using ACF PRO, so ACF5. I switched out $_POST['fields'] with $_POST['acf'] but unfortunately my code is still not working. Saving an post as featured, doesn’t unset the featured option on a different post.

    So my code now looks like:

    function my_acf_save_post( $post_id ){
    	
    	$fields = false;
    	
    	if ( isset( $_POST['acf'] ) ) {
    		$fieldId = 'field_5b51ec43f331a';
    		$fields = $_POST['acf'];
    
    		$featuredArticle = $fields[$fieldId];
    		
    		if ( $featuredArticle ) {
    			$args = array(
    				'post_type' => array('post'),
    				'meta_query' => array(
    					array(
    						'key' => 'featured',
    						'value' => '1',
    						'compare' => '=='
    					)
    				)
    			);
    			
    			$post_query = new WP_Query( $args );
    			
    			if ( $post_query->have_posts() ) {
    				while ( $post_query->have_posts() ) {
    					$post_query->the_post();
    					
    					acf_update_field( $fieldId, false, get_the_ID() );
    					
    				}
    			}
    		}
    	}
    
    } // end my_acf_save_post
    add_action('acf/save_post', 'my_acf_save_post', 1);