Support

Account

Home Forums ACF PRO acf_form 'acf/save_post' performance

Solved

acf_form 'acf/save_post' performance

  • Howdy,

    I’m running multiple (a lot) of acf_form instances on the front-end to create/edit various posts.

    I’ve noticed that with the increase of forms that I have, that the time taken to save/execute has increased.

    I’m curious to know: a) is it better to run one acf/save_post function with conditionals or b) multiple acf/save_post functions for each instance.

    For instance:

    One acf/save_post function with conditionals):

    
    function my_acf_save($post_id){
    	$current_user_id = get_current_user_id();
    	if(!is_admin()){
    		if(is_page(123)){
    			// do stuff
    			update_post_meta($post_id,'my_meta_field1',$acf_field1);
    		};
    		if(is_page(456)){
    			update_user_meta($current_user_id,'my_user_meta_field',$acf_field2);
    			update_post_meta($post_id,'my_meta_field2',$acf_field3);
    		};
    	};
    };
    add_action('acf/save_post', 'mjua_acf_save', 20);
    
    <strong>Multiple acf/save_post functions for each instance</strong>
    
    

    function my_acf_save1($post_id){
    $current_user_id = get_current_user_id();
    if(!is_admin()){
    if(is_page(456)){
    update_user_meta($current_user_id,’my_user_meta_field’,$acf_field2);
    update_post_meta($post_id,’my_meta_field2′,$acf_field3);
    };
    };
    };
    add_action(‘acf/save_post’, ‘mjua_acf_save1’, 20);

    
    
    

    function my_acf_save3($post_id){
    $current_user_id = get_current_user_id();
    if(!is_admin()){
    if(is_page(456)){
    update_user_meta($current_user_id,’my_user_meta_field’,$acf_field2);
    update_post_meta($post_id,’my_meta_field2′,$acf_field3);
    };
    };
    };
    add_action(‘acf/save_post’, ‘mjua_acf_save3’, 20);
    `

    Thanks

  • My question would be, why are you calling update_post_meta() for every field on the acf_form(). because you are using an acf/save_post filter ACF has already save these values somewhere. If you are then saving the values somewhere else as well then you are doubling the amount of DB queries that need to be performed.

    It really does not matter if you do this in one function or multiple functions, the result will be pretty much the same. It’s not the number of functions that need to be called, this will hardly effect performance unless you have thousands of filters.

    Maybe you should explain why you’re doing all of these DB updates and maybe there is a better way.

  • Hi John,

    Thanks for this and thanks for the clarification. (also apologies for my delayed response).

    I’ve since updated my functions to reduce post_meta functions. However, I use acf_forms in multiple locations and I have them perform multi-relational actions (not just on the post_id itself, but rather ‘connected’ ones)…either way I’m rolling out functions to simplify this. Essentially, based on the page within which I embed an acf_form, I need acf/save_post to perform different actions without interfering with each other.

    My main question was if it was better (more performant) to use conditionals within one add_action hook or have multiple add_action hooks, i.e.

    
    function my_acf_save_post( $post_id ) {
        
        if($post_id = 1){
    	    // do something 1
        }
        if($post_id = 2){
    	    // do something 1
        }
        if($post_id = 3){
    	    // do something 1
        }
        if($post_id = 4){
    	    // do something 1
        } 
        
    }
    
    add_action('acf/save_post', 'my_acf_save_post', 20);
    

    vs.

    
    function my_acf_save_post_1( $post_id ) {
        
        // do something 1
    }
    
    add_action('acf/save_post', 'my_acf_save_post_1', 20);
    
    function my_acf_save_post_2( $post_id ) {
        
        // do something 2
    }
    
    add_action('acf/save_post', 'my_acf_save_post_2', 20);
    
    function my_acf_save_post_3( $post_id ) {
        
        // do something 3
    }
    
    add_action('acf/save_post', 'my_acf_save_post_3', 20);
    
    function my_acf_save_post_4( $post_id ) {
        
        // do something 4
    }
    
    add_action('acf/save_post', 'my_acf_save_post_4', 20);
    

    I personally prefer multiple ‘if’ functions because I’ve found that without that some forms seem to interfere with each other (I have a lot of them and sometimes multiple forms on the same page).

  • In the long run, if it was run thousands of times in a single page load, the first example will perform slightly better than the second. The WP filter system and calling many functions takes a bit more time than calling one function. However, while I don’t have any way to test this, I suspect that the time involved would be measured in microseconds. Like I said, you’d need to have thousands of functions/filters to really make a difference. If you prefer one function over many than that’s the way you should go. For me it would be a matter of what will be easier for me to figure out when I look at it in a year because I need to change something.

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

The topic ‘acf_form 'acf/save_post' performance’ is closed to new replies.