Support

Account

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

Solved

True/False – Only allow one post to have true value

  • Hello,

    I created a True/False field for “featured” on my posts. On my blog archive page, I’m using WP_Query to pull in 1 post (default ordering/orderby) that has the “featured” field checked to display differently from all the other posts on my blog archive page.

    Then I’m using Search & Filter Pro (S&F) to load the rest of the posts below the featured. I have a rule set in my S&F form to exclude any posts where “featured” had a value of 1 (true). I don’t have a way to only exclude only the most recently published post where featured is true, just all that are set to true.

    So what I’m hoping to accomplish is when a new post is created with a value of true for featured, any other post with featured set to true, gets set to false. So that there is only one featured post at any given time.

    I found this support topic where someone was wanting to do the exact same thing, and actually found a solution that worked for him, but is not working for me.

    I’m not sure if something has changed since 2014 and his code is no longer valid, but I’ve been playing around with his code, as well as reading the documentation for acf/save_post but aren’t having any luck getting it to work.

    Here is a version of my code that I have tried, it is basically the same as the guy’s code from the topic I linked to above, I just removed the check for the category and replaced his field key with mine.

    function my_acf_save_post( $post_id ){
    	$fields = false;
    	// Check if Fields have been posted
    	if ( isset( $_POST['fields'] ) ) {
    		$fieldId = 'field_5b51ec43f331a';
    		$fields = $_POST['fields'];
    		// Store the true/false field on a variable (change the "Field_xxx" id by yours)
    		$featuredArticle = $fields[$fieldId];
    		// Check if the true/false field is true
    		if ( $featuredArticle ) {
    			/* If it's set to true do a WP_query so you can get the other post that have $featuredArticle set to true here you may want to change the 'cat' to whatever your is (or to all) in the meta query you should use your true/false field name as a key */
    			$args = array(
    				'post_type' => array('post'),
    				'meta_query' => array(
    					array(
    						'key' => 'featured',
    						'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() ) {
    					$post_query->the_post();
    					/* For each posts we update the true/false field and set it to false */
    					update_field( $fieldId, false, get_the_ID() );
    				}
    			}
    		}
    	}
    
    } // end my_acf_save_post
    add_action('acf/save_post', 'my_acf_save_post', 1);

    I’ve tried changing a few things, including the priority on the action to above 10, but like I said, nothing I do seems to work.

    Any help would be greatly appreciated.

    Thanks

  • Are you using ACF 4 or ACF 5. There is some difference in the $_POST array for ACF 5. In ACF 5 the fields are in $_POST['acf'] instead of $_POST['fields']

  • 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);
  • Hi John,

    Do you have any other thoughts or recommendations?

    Thanks

  • Sorry for not getting back here sooner.

    I just noticed that you are calling

    
    acf_update_field( $fieldId, false, get_the_ID() );
    

    That function does not update the value of the field, it updates the acf field’s settings. You should be using update_field();

  • That did it! You da man, John!

    I really appreciate your help. Thanks again.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘True/False – Only allow one post to have true value’ is closed to new replies.