Support

Account

Home Forums Backend Issues (wp-admin) Why isn't acf/save_post firing?

Solved

Why isn't acf/save_post firing?

  • I’m coming into an existing system and I see where the previous programmer has two add_action calls:

    add_action( 'transition_post_status', 'send_checklist_publish', 20, 3 );

    and

    add_action( 'acf/save_post', 'send_checklist_publish', 20 );

    The ‘send_checklist_publish’ function sends an email out.

    I was wondering why the duplication in the hooks, but once I commented out the first line, I realized that the ‘acf/save_post’ action isn’t firing at all.

    Does anyone know any reason why this may be happening? I’d prefer to use this action over the transition_post_status, since the get_field() value being returned isn’t the new information.

    Thanks!

  • Hi @yazminmedia

    Could you please share the send_checklist_publish function code?

    You can also try to update a field using that hook to see if the hook is fired or not. This page should give you more idea about it: http://www.advancedcustomfields.com/resources/update_field/.

    Thanks!

  • Yes, of course. Here is the function code:

    	function send_checklist_publish( $new_status, $old_status, $post )
    	{
    	    if ( 'publish' !== $new_status
    	        or 'checklist' !== get_post_type( $post ) )
    	        return;
    	
    
    				$post_type = get_post_type( $post ); //get post type
    
    				$titles = new WP_Query(array('post_type' => $post_type, 'p' => $post->ID));
    				$titles->the_post();
    				$categories = get_the_category();
    				$category = $categories[0]->name;
    				$response = get_the_terms( $post->ID, 'Response_Category' );
    				$problematic = get_field('would_you_consider_this_film_problematic');
                    $subject_line = $_POST['fields']['subject_line'];
    				if($problematic == 'Yes'):
    					$problematic_response = "*Be advised upon ingest, it was recognized that this title is problematic.* <br/><br/>";
    				endif;
    
    				//Get Author's Name
    				$userID =  get_the_author_id();
    				$user_info = get_userdata( $userID );
    				$first_name = $user_info->first_name;
    				$last_name = $user_info->last_name;
    
    				if($response[0]->slug == "send-check-list-email"){ //Send email if checked
    
    			        $lists = array(...); // This is an array of all of our email addresses. Pulled to avoid spam, and already verified this is not the code causing the issue.
    
    				$email = array();
    				$terms = get_the_terms( $post->ID, 'Distro_Category' );
    				foreach ( $terms as $term ) {
    
    				if (array_key_exists($term->name,$lists))
    				   {
    				  	$term_name = $term->name;
    
    					$i = 0;
    					foreach($lists[$term_name] as $list){ //Loop through users on the Distro List
    
    					$getUser = get_user_by( 'email', $list );
    					$networks = get_user_meta( $getUser->ID, 'networks', true );
    
    					if(in_array($category,$networks)){
    						$emails[] = $list;
    						}
    
    							$i++;
    				   	 }  //End foreach
    
    				   } 
    
    				}
    
    				$postid = get_the_ID();
    				//$postid = 30104;
    				
    
    				$body =  'Hello:
    
    				<br/><br/>
    
    				'.get_the_title().' has been ingested and placed in ISIS. You can view the checklist here:
    				<br/><br/>
    
    				<a href='.get_permalink($post->ID).'>'.get_permalink($post->ID).'</a>
    				<br/><br/>
    				
    				'.$problematic_response.'
    			
    				Thank You, <br/>
    				'.$first_name.' '.$last_name.' <br/>
    				Ingest Ops';
    
    				//Email Headers
    				$headers = "From:  Network Database <[email protected]>";
    
                    wp_mail( $emails, $subject_line . ': '.get_the_title().'', $body, $headers );
    
    				} 
    
    	}

    Also, I don’t mind adding the update_field function in to test, but since both add_actions are calling the same function (send_checklist_publish), wouldn’t it be correct to believe that the acf/save_post hook isn’t getting fired since it doesn’t send out an email when the transition_post_status does send out an email? Or is there something I’m missing.

    Thanks!

  • Hi @yazminmedia

    As you can see in the code, the “transition_post_status” hook provides three arguments for the send_checklist_publish() function while the “acf/save_post” hook provides only one argument, which is the post ID. This page should give you more idea about it: http://www.advancedcustomfields.com/resources/acfsave_post/.

    In the send_checklist_publish() function, it checks the $new_status variable, which is set to post ID by the “acf/save_post” hook. Thus, it will return an empty response because the post ID is not the same as “publish”.

    I believe using the “transition_post_status” is a better way to do it.

    I hope this makes sense.

  • Thanks James@acf-support –

    I did read that and thought I understood, but clearly I’m not as I missed the difference in the number of arguments. I’ll go back and re-read in the morning.

    This still leaves me with the original issue of how to access the new data for the custom fields since “transition_post_status” seems to be firing before the data is actually saved.

    Is there a way to access that data? get_field() is pulling the existing db values…

    Thanks!

  • Hi @yazminmedia

    You can get the existing custom field value using the get_field() function and the new value using $_POST["acf"]["field_1234567890"], where “field_1234567890” is the field key of your custom field.

    I hope this helps 🙂

  • Thank you James@acf-support!

    That worked perfectly.

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

The topic ‘Why isn't acf/save_post firing?’ is closed to new replies.