Home Forums Backend Issues (wp-admin) True/false – When new true, update the rest to false Reply To: True/false – When new true, update the rest to false

  • Hi that’s exactly what i had to do for a project, mine was about shoes but that’s how i set it up:

    When you Publish a post if the Featured checkbox on the post is set to true the previous Featured post is set to false so you always have a single featured post.

    basically that post runs on every acf/save_post and runs before the data are saved.
    The code check if you set the true/false option for the post you are saving.
    if you did it makes a query (in my example restricted by post type) to see if there is any featured posts. If there is it will set the featured post value to false, if there isn’t it will do nothing, in both cases it will end up setting the featured value of the saved post to true.

    //First you would have to add acf/save_post action to you function.php so the action runs every time a post is saved or udpated 
    // run before ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);	
    function my_acf_save_post( $post_id ){
    	// vars
    	$fields = false;
     //here i added a if stattement so the code runs only for a defined post //type you can change "pompitup_product" or delet that if statement
    if(get_post_type( $post_id ) === "pompitup_product"){
    	//check if Fields have been posted
    	if( isset($_POST['fields']) ){
    		//store the true/false field on a variable (change 
                    //the "Field_xxx" id by yours)
    		$feature_product = $fields[field_52b2c5c9d8267];
    			//check if the true/false field is true
    			if( $feature_product ){
    		     //if it's set to true do a WP_query so you can get 
                        //the other post that have $feature_product set to true
    	          //here you may want to change the 'post type' to 
                    //whatever your is (or to all)
    		//in the meta query you sholud use your 
                    // true/false field name as a key
                   $args = array(	'post_type' => 'pompitup_product', 
    				'meta_query' => array( 
                                            'key' => 'produit_mis_en_avant', 
    					'value' => '1', 
    					'compare' => '==')),);
                   $post_query = new WP_Query($args);
    		//here we check if the query returns post (if it does 
                   //it will return featured posts
                  if($post_query->have_posts() ) {
    		  while($post_query->have_posts() ) {
                            //here we get the featured post id
    			//for each posts we update the true/false 
                           //field and set it to false 
                         //again use your own field_xxx here
    		update_field('field_52b2c5c9d8267', false, get_the_ID());
    		}//end isset($_POST['fields'])---->for post
     	}//end check if post-type== pompitup_product
    }//end my_acf_save_post

    sorry about the bad indentation, I find it really hard to past code in there.

    I think there is a cleaner way to do that but that gets it working.