Support

Account

Home Forums Backend Issues (wp-admin) Why doesn't this work on acf/save_post when attaching gallery ids?

Solving

Why doesn't this work on acf/save_post when attaching gallery ids?

  • 		function on_post_save( $post_id, $post, $update) {
    
    			// Get previous values.
    			//$prev_values = get_fields( $post_id );
    		
    			// Get submitted values.
    			//$values = $_POST['acf'];
    
    	    $post_content = html_entity_decode($post->post_content);
                // post_content contains my content with 2 links:
                // <a href='#' data-id='59'>Gallery pop 1 id: 59</a>
                // <a href='#' data-id='55'>Gallery pop 2 id: 55</a>
    
                $dom = new DomDocument();
                @$dom->loadHTML($post_content);
                
                $attachment_ids = array();
                foreach ($dom->getElementsByTagName('a') as $item) {
                   $attachment_ids[] = $item->getAttribute('data-id');
                }
        
                //var_dump($attachment_ids);
    			// die();
      
                if($attachment_ids){
                    ///field key for my ACF Gallery field
                    update_field('field_63a6bd5d2a3d2',$attachment_ids, $post_id);
                }else{
                    update_field('field_63a6bd5d2a3d2',$attachment_ids,$post_id);
                }
    	
    		}
    		add_action('save_post', 'on_post_save',1,5);
  • Does the field with the key field_63a6bd5d2a3d2 actually exist on the page/post that is being submitted?

    Assuming this is the case.

    Your save_post action has a priority of 1. ACF runs on the save_post hook with a priority of 10. Your action is running before ACF saves field values. When ACF saves the field values it updates the same field with whatever was submitted in the form, overwriting what you did.

    This is why it is important to use the acf/save_post hook instead of the standard save_post hook with the correct priority to run either before or after ACF has saved values.

  • Ok I see.. and yes that field actually exists..
    I guess i see because when I do a vardump I see that the load dom is parsing the ids. It’s just when it gets to the part to update the fields is what’s not working.
    Does acf/save_post have these parameters: $post_id, $post
    Or would I have to called for globals?

  • acf/save_post only passes the post ID. With that you can get the post do what you are trying to do.

  • Ok thanks!! John..
    This is what works!!

    function on_post_save( $post_id) {
    
    			//Get previous values.
    			//$prev_values = get_fields( $post_id );
    		
    			//Get submitted values.
    			//$values = $_POST['acf'];
    
    			$post   = get_post( $post_id );
                $output =  apply_filters( 'the_content', $post->post_content );
    			if($output):
    					
    					$post_content = html_entity_decode($output);
    					$dom = new DomDocument();
    					@$dom->loadHTML($post_content);
    					
    					$attachment_ids = array();
    					foreach ($dom->getElementsByTagName('a') as $item) {
    					$attachment_ids[] = $item->getAttribute('data-id');
    					}
    			
    					//var_dump($attachment_ids);
    					// die();
    				
    					if($attachment_ids):
    						update_field('field_63a6bd5d2a3d2',$attachment_ids, $post_id);
    					else:
    						update_field('field_63a6bd5d2a3d2',"", $post_id);
    					endif;
    			else:	
    				   //clear all attachments if no content is available 	
    				   update_field('field_63a6bd5d2a3d2',"", $post_id);
    			endif; 
    		}
    		add_action('acf/save_post', 'on_post_save', 10);
  • @hube2 John Huebner
    I am a big fan of you and your ACF addon plugins..
    I actually took one of your plugins and made a new ACF Post Status addon out of it!
    here: https://github.com/samjco/acf-show-field-by-status-rules

    Maybe one day we may cross paths at a WordCamp or something! =)

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

You must be logged in to reply to this topic.