简体   繁体   中英

Retrieve meta field values of ACF flexible content items using get_post_meta - WordPress

Thanks for your help in advance. Here's what I'm trying to achieve:

I have a custom post type called 'Campaigns' and I have a custom taxonomy called 'Countries' that is related to the campaign custom post type. When a user adds a new country to a campaign a new campaign post is generated that is the child of the current campaign. I'm duplicating the ACF fields that are assigned to the parent campaign and replicating the values in the child post, however I've run into an issue using the ACF flexible content fields. Here'a snippet of my code that is retrieving the parent post fields and updating the newly created ACF field in the child post with that value.

$action_text = get_post_meta($parent_id, 'action_text', true);
update_field('action_text', $action_text, $post_id);

I've tried doing this with flexible content, but I know I need to loop through and find what content blocks have been created. What is the best way to go about this?

// About Fields
        $about_fields = get_post_meta($parent_id, 'content');
        var_dump($about_fields);
        $meta_key = // How to retrieve the flexible content keys
        $meta_value_of_flexible_content = get_post_meta($parent_id, $meta_key);

        if($about_fields) {

        }

For clarification 'content' is the flexible container name. 'text_and_image' is an example name of one of the flexible content blocks I've created.

Thanks again for any insights.

I've tried doing this with flexible content, but I know I need to loop through and find what content blocks have been created.

You could just use the get_field() and update_field() functions to duplicate any ACF fields, including Flexible Content fields.

So for example, to clone the whole content field:

$about_fields = get_field( 'content', $parent_id );
if ( $about_fields ) {
    update_field( 'content', $about_fields, $post_id );
}

// How to retrieve the flexible content keys

foreach ( $about_fields as $arr ) {
    echo 'Layout: ' . $arr['acf_fc_layout']; // e.g. "Layout: text_and_image"

    // The rest of items in `$arr` are the SUB-fields of that specific layout as
    // identified by the `$arr['acf_fc_layout']`, which is the layout's name. So
    // if you have two SUB-fields named `text1` and `image1` respectively, then
    // these items are set: `$arr['text1']` and `$arr['image1']`
}

Additional Code

To clone all ACF fields:

$fields = get_fields( $parent_id );
foreach ( $fields as $name => $value ) {
    update_field( $name, $value, $post_id );
}

Additional Note

I'd change this to use the get_field() function:

$action_text = get_post_meta($parent_id, 'action_text', true);

So:

$action_text = get_field('action_text', $parent_id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM