Home Forums General Issues Updating ACF fields not in a post


Updating ACF fields not in a post

  • I am currently trying to sync BuddyPress xprofile data to a post that belongs to the current logged in user.
    I can get all the relevant information (such as the current user, the field I want to update, the post id I need to update, etc) but it does not fail, but does not update the post.
    At no stage am I loading a post to edit per se, this happens when saving the changes on the buddypress user profile

    Here is the code I am using:

    //get the Advanced custom field key
    function get_acf_key($field_name) {
      global $wpdb;
      $length = strlen($field_name);
      $sql = "
        SELECT <code>meta_key</code>
        FROM {$wpdb->postmeta}
        WHERE <code>meta_key</code> LIKE 'field_%' AND <code>meta_value</code> LIKE '%\"name\";s:$length:\"$field_name\";%';
      return $wpdb->get_var($sql);
    function syncbuddypresstoacf() {
    //Need to add stuff here to sync to correct user post
        //Need to check for two (if allowing multiple member types)
        if ( is_user_logged_in() ) {
        // Current user is logged in,
        // so let's get current user info
        $current_user = wp_get_current_user();
        // User name
        $user_name = $current_user->user_login;
        $user_id = $current_user->ID;
    $posts = get_posts(array(
    	'numberposts'	=> -1,
    	'post_type'		=> array('babysitters','parents_post_type'),
    	'meta_key'		=> 'username',
    	'meta_value'	=> $user_name
    //Get page postid
        $post_id = $posts['0']->ID;
      //Get Value From BuddyPress
      $bio =   bp_profile_field_data('field=Biography')  ;
    //Update ACF    
        $field_name = 'biography';
    update_field(get_acf_key($field_name), $bio, $post_id);
    add_action( 'xprofile_data_after_save', 'syncbuddypresstoacf' );	  

    Excuse the die comment, was just wanting to see the values when they are returned.

  • Hi @canobeans

    Could you please make sure that the field key is correct? Could you please try to update it manually like this:

    update_field('field_123456789abc', 'the value', '99');

    Where ‘field_123456789abc’ is the field key you want to update and ’99’ is the post ID you want to update.

    Thanks 🙂

  • That saved the information, when hard coding it like you suggested .
    The field key is correct . Would the biography information require single quotation marks around it to successfully submit?

  • Hi @canobeans

    No, it should be OK with both single or double quotes.

    Could you please share the result of the var_dump?


    Also, could you please share the JSON or XML export file of your field group?

    Thanks 🙂

  • Here is the attached XML file.

    I can see the issue with the buddypress function I have used, is that it echoes straight to the screen, but not into the variable I have used. Anyway, here are the var_dumps

    $field_name = string(19) “field_57c39d2c478be”
    $bio = NULL
    $post_id = int(2235)

    I used the incorrect function I had bp_profile_field_data when it should have been bp_get_profile_field_data. Now all outputs and updates correctly.

    Now to work on the remainder of the fields to get them to update!!

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

The topic ‘Updating ACF fields not in a post’ is closed to new replies.