Support

Account

Home Forums Backend Issues (wp-admin) Saving post meta from ACF directly into DB Doesn't work without update press.

Solved

Saving post meta from ACF directly into DB Doesn't work without update press.

  • Hi there. Hoping someone can help!

    I have a issue with flexible content fields.

    I’ve gone into the database, copied out the fields it generates on the page (including the _ key fields and the flexible content one itself (has: a:1:{i:0;s:36:”content_panel_accommodation_features”;}) inside.

    Now when I to the insert into the post_meta table it works (other then for some reason add_post_meta adding s:xx( to the start of the json string for that field). But got around that with WPDB.

    However, when you view the editor page, it shows up all the fields, however on the template it doesn’t appear to have saved it correctly (as it’s showing up all the fields as arrays in a var dump).

    This get’s corrected when i click ‘update’ on the post… but need it to work without needing to do that.. any ideas?

  • Hey there,

    This sounds like a complicated process, I wonder if we can simplify? Are you aware of the following ACF functionality:

    This sounds like it might be more helpful. Particularly if you’re trying to make ACF fields/groups transportable. I personally use the acf-json method which means that the fields I create locally are version controlled and can be deployed to remote servers without me needing to edit databases.

    I hope this helps.

  • Why are you trying to insert content for ACF directly into the DB?

  • I’ve created a plugin which does a ‘fresh sync’ from an API, which destroys the old posts, and re-creates them. I then want to make a generic structure to be placed back in by default.

  • For others i worked out the solution.

    So essentially you use the update_field, then update_post.

    Example:

    
    // Flexible element
    $fc_key = "field_key_for_flexible_content";
    
    // build the rows to insert
    
    $fc_rows = array(
      array(
                'field_key_for_field_inside_flexible_layout' => '',
                'acf_fc_layout' => 'name_of_layout_inside_flexible_item'
            ),
      array(
                'field_key_for_field_inside_flexible_layout' => '',
                'acf_fc_layout' => 'name_of_layout_inside_flexible_item'
            ),
    );
    
    // run the update
    update_field( $fc_key, $fc_rows, $post_id );
    
    // update
    wp_update_post( array (
      'ID' => $post_id,
    ));
    
  • Apologies, my response talked about capturing the fields themselves rather than the related data. This is what happens when you answer questions late at night after a couple of beers. Interesting question though and good of you to post the solution, which is, coincidently, going to be useful to me in the near future.

  • No that’s fine, thanks for replying, i appreciate it 🙂

    I think the process of this working, will be very useful for me moving forward, as i want to auto populate some fields etc, so gonna see just how it works for ‘options page’ fields.

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

The topic ‘Saving post meta from ACF directly into DB Doesn't work without update press.’ is closed to new replies.