Support

Account

Home Forums ACF PRO Uncheck checkbox programmatically

Solving

Uncheck checkbox programmatically

  • Hi guys,

    I’m trying to uncheck a single checkbox. This does not work as expected.

    So, I’m having a checkbox in posts. When the post get’s published and the checkbox is checked, I do something. Afterwards I want to uncheck the checkbox via API, so that the checkbox is unchecked if the post gets unpublished and published for some reason.
    None of the following work.

    
    update_field("my_checkbox",0,$postid);
    update_field("my_checkbox",array(),$postid);
    update_field("my_checkbox",false,$postid);
    

    The checkbox has a single option “true : Yes”.

    Any ideas why how to do that?

    Best regards Sascha

  • Are you using a checkbox field or a true false field? What are the field settings?

  • Hi John,
    I’m using a checkbox. But the main issue I found over the weekend seems to be, that I hook into post transition status. That seems to fire PRIOR to saving fields. So, the check I do in “sendPublishNewseletter” -> “if(get_field(“send_newsletter”,$postid))” first of all is only true, if the checkbox was saved before. So I guess the “update_field(“send_newsletter”,0,$postid);” function does nothing, as it does not save to the database???

    See my full code below.

    Thanks Sascha

    P.S.: I try to send a newsletter out, when the post is published. This is done by the “do_action(news_publish)” found later in the code.

    
    /* Add Newsletter Action Hook on Publish */
    add_action('transition_post_status', 'send_new_post', 10, 3);
    // Listen for publishing of a new post
    function send_new_post($new_status, $old_status, $post) {
      if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {
        sendPublishNewseletter($post->ID);
      }
    }
    //sending function
    function sendPublishNewseletter($postid){
    	if(get_field("send_newsletter",$postid)){
    		//uncheck field
    		update_field("send_newsletter",0,$postid);
    		
    		//define post ID
    		$sendoutID = $postid;
    		//get files
    		$downloadHTML ='';
    		if(get_field("dateien",$sendoutID)) { 
    				$downloadHTML .= '<div style="margin: 20px; border: 1px solid #000; border-radius: 3px; background-color: #ECECEC;"><div style="margin: 20px; "><h3>Dokumente zum Download</h3>';
    				foreach(get_field("dateien",$sendoutID) as $singleFile) {
    					$thumbnailURL = '/wp-includes/images/media/document.png';
    					$downloadHTML .= '<a href="'.$singleFile['datei']['url'].'" class="textlink" download>'.$singleFile['datei']['title'].' ('.pathinfo($singleFile['datei']['url'],PATHINFO_EXTENSION).', '.size_format($singleFile['datei']['filesize']).')</a><br/>';
    				}
    				$downloadHTML .= '</div></div>';
    			}
    		
            do_action( 'news_publish','', array( 
    			'teaser-subject' => '{post_title:'.$sendoutID.'}',
    			'teaser-date' => get_the_date( 'd.m.Y', $sendoutID ),
    			'teaser-image' => get_the_post_thumbnail_url($sendoutID),
    			'teaser-link' => '{post_link:'.$sendoutID.'}',
    			'teaser-content' => '{post_content:'.$sendoutID.'}',
    			'teaser-downloads' => '<div style="text-align: center;">'.$downloadHTML.'</div>',
    			
    		));
    	}
    }
    
  • Yes, you cannot do anything with ACF fields from the DB until after ACF saves the values and acf has not done so when the hook you are using runs, so this is returning the old value and not the updated value

    
    if(get_field("send_newsletter",$postid)){
    

    in order to do things base on acf fields you need to do one of two things

    The first it to use the acf/save_post hook to run your code.

    The second is to look in $_POST[‘acf’] to check field values.

    See this for more information on both https://www.advancedcustomfields.com/resources/acf-save_post/

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

The topic ‘Uncheck checkbox programmatically’ is closed to new replies.