Support

Account

Home Forums General Issues update_field() with Ajax Reply To: update_field() with Ajax

  • In your second code snippet, you will need to provide the post ID of the post you’re updating.

    
    function test_function() {
    	$input_test = $_POST['input-test'];
    	if (!isset($input_test) || $input_test == "") { $input_test = "Fall Back"; }
    	update_field('field-name', $input_test, $post_id); // specify post ID
    	wp_die();
    }
    add_action( 'wp_ajax_nopriv_test_function',  'test_function' );
    add_action( 'wp_ajax_test_function','test_function' );
    

    You will either need to add a hidden field your form that has the post ID and submits it or you will need to localize your script with the current post ID that you can add to the form submission. I’d probably go with the hidden post ID field because of the way you script submits the form.