Support

Account

Home Forums ACF PRO wp_insert_post() and populating ACF Blocks

Solving

wp_insert_post() and populating ACF Blocks

  • Hi all,

    I’ve inherited a site from a previous developer which uses ACF and specifically for this issue ACF Blocks for creating posts.

    I need to programmatically create a few hundred new pages from a PHP function.

    I have this working to create the actual pages but strugging to find an example of how to add a heading block to the created pages and populate it’s content during the wp_insert_page() call or afterwards.

    Can anyone help me out?

    My current function is simply:

    $createPage = array(
    ‘post_title’ => $pageName,
    ‘post_content’ => ”,
    ‘post_status’ => ‘publish’,
    ‘post_author’ => 1,
    ‘post_type’ => ‘page’,
    ‘post_name’ => ‘product-location-guide’
    );

    // Insert the post into the database
    wp_insert_post( $createPage );

    This creates the page and I can manually then add the blocks and content but need to do it all from PHP.

  • Just realised that this isn’t actually blocks – rather ACF flexible content fields named ‘blocks’ in the site set-up.

    So the question actually is – can I programatically create a flexible content field and populate it from PHP?

    • onion

    • January 30, 2020 at 7:00 pm

    I also get this problem.

    how do I use ACF block to add wp_insert_post post_content?

    • onion

    • January 30, 2020 at 7:15 pm

    here is my acf block settings

    
    {
        "key": "group_5c36cf493f91b",
        "title": "My - Payment Options",
        "fields": [
            {
                "key": "field_5c36cf5487c08",
                "label": "Payment Options",
                "name": "my_land_payment_options",
                "type": "repeater",
                "instructions": "First and second payment option will display in the land grid. The first option will be shown in colors. The second payment option will be a line of smaller black text.",
                "required": 0,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "collapsed": "",
                "min": 1,
                "max": 0,
                "layout": "block",
                "button_label": "Add Option",
                "sub_fields": [
                    {
                        "key": "field_5c36cf7387c09",
                        "label": "Payment Title",
                        "name": "my_land_payment_options_title",
                        "type": "text",
                        "instructions": "",
                        "required": 1,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "Cash Price",
                        "placeholder": "Cash Price",
                        "prepend": "",
                        "append": "",
                        "maxlength": ""
                    },
                    {
                        "key": "field_5c36cf8787c0a",
                        "label": "Price",
                        "name": "my_land_payment_options_price",
                        "type": "text",
                        "instructions": "",
                        "required": 1,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "$1234",
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "maxlength": ""
                    },
                    {
                        "key": "field_5c36cf9f87c0b",
                        "label": "Price Note",
                        "name": "my_land_payment_options_price_note",
                        "type": "text",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "",
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "maxlength": ""
                    },
                    {
                        "key": "field_5c6cc891d1407",
                        "label": "Cross out price note",
                        "name": "my_land_payment_options_price_note_cross",
                        "type": "true_false",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "message": "",
                        "default_value": 0,
                        "ui": 0,
                        "ui_on_text": "",
                        "ui_off_text": ""
                    },
                    {
                        "key": "field_5c36cfae87c0c",
                        "label": "Payment Note",
                        "name": "my_land_payment_options_note",
                        "type": "text",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "",
                        "placeholder": "down payment 5%",
                        "prepend": "",
                        "append": "",
                        "maxlength": ""
                    },
                    {
                        "key": "field_5c36cfc287c0d",
                        "label": "Button Text",
                        "name": "my_land_payment_options_button_text",
                        "type": "text",
                        "instructions": "",
                        "required": 1,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "Reserve Now",
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "maxlength": ""
                    },
                    {
                        "key": "field_5cf7598bfd0dd",
                        "label": "Button Options",
                        "name": "my_land_payment_options_button_select",
                        "type": "select",
                        "instructions": "",
                        "required": 1,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "choices": {
                            "button": "Custom Button",
                            "authorize": "Authorize.Net"
                        },
                        "default_value": [],
                        "allow_null": 0,
                        "multiple": 0,
                        "ui": 0,
                        "return_format": "value",
                        "ajax": 0,
                        "placeholder": ""
                    },
                    {
                        "key": "field_5cf75a2dfd0de",
                        "label": "Payment Type",
                        "name": "my_land_payment_options_authorize_payment_type",
                        "type": "select",
                        "instructions": "",
                        "required": 1,
                        "conditional_logic": [
                            [
                                {
                                    "field": "field_5cf7598bfd0dd",
                                    "operator": "==",
                                    "value": "authorize"
                                }
                            ]
                        ],
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "choices": {
                            "one time payment": "One Time",
                            "recurring payment": "Recurring"
                        },
                        "default_value": [],
                        "allow_null": 0,
                        "multiple": 0,
                        "ui": 0,
                        "return_format": "value",
                        "ajax": 0,
                        "placeholder": ""
                    },
                    {
                        "key": "field_5cf75afcfd0df",
                        "label": "Payment Amount",
                        "name": "my_land_payment_options_authorize_payment_price",
                        "type": "number",
                        "instructions": "",
                        "required": 1,
                        "conditional_logic": [
                            [
                                {
                                    "field": "field_5cf7598bfd0dd",
                                    "operator": "==",
                                    "value": "authorize"
                                }
                            ]
                        ],
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "",
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "min": "",
                        "max": "",
                        "step": ""
                    },
                    {
                        "key": "field_5d138724aaa98",
                        "label": "Interval Length",
                        "name": "my_land_payment_options_authorize_recurring_length",
                        "type": "number",
                        "instructions": "For an interval unit of days, use an integer between 7 and 365, inclusive. For an interval unit of months, use an integer between 1 and 12, inclusive.",
                        "required": 1,
                        "conditional_logic": [
                            [
                                {
                                    "field": "field_5cf7598bfd0dd",
                                    "operator": "==",
                                    "value": "authorize"
                                },
                                {
                                    "field": "field_5cf75a2dfd0de",
                                    "operator": "==",
                                    "value": "recurring payment"
                                }
                            ]
                        ],
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": 1,
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "min": 1,
                        "max": 365,
                        "step": ""
                    },
                    {
                        "key": "field_5cf75b5efd0e0",
                        "label": "Interval Unit",
                        "name": "my_land_payment_options_authorize_recurring_type",
                        "type": "select",
                        "instructions": "",
                        "required": 1,
                        "conditional_logic": [
                            [
                                {
                                    "field": "field_5cf7598bfd0dd",
                                    "operator": "==",
                                    "value": "authorize"
                                },
                                {
                                    "field": "field_5cf75a2dfd0de",
                                    "operator": "==",
                                    "value": "recurring payment"
                                }
                            ]
                        ],
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "choices": {
                            "days": "day(s)",
                            "weeks": "week(s)",
                            "months": "month(s)"
                        },
                        "default_value": [],
                        "allow_null": 0,
                        "multiple": 0,
                        "ui": 0,
                        "return_format": "value",
                        "ajax": 0,
                        "placeholder": ""
                    },
                    {
                        "key": "field_5cf75be3fd0e2",
                        "label": "Total Occurrences",
                        "name": "my_land_payment_options_authorize_recurring_times",
                        "type": "number",
                        "instructions": "",
                        "required": 1,
                        "conditional_logic": [
                            [
                                {
                                    "field": "field_5cf7598bfd0dd",
                                    "operator": "==",
                                    "value": "authorize"
                                },
                                {
                                    "field": "field_5cf75a2dfd0de",
                                    "operator": "==",
                                    "value": "recurring payment"
                                }
                            ]
                        ],
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": 1,
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "min": 1,
                        "max": 9999,
                        "step": ""
                    },
                    {
                        "key": "field_5c6df879433e8",
                        "label": "Button Link",
                        "name": "my_land_payment_options_button_link",
                        "type": "url",
                        "instructions": "",
                        "required": 1,
                        "conditional_logic": [
                            [
                                {
                                    "field": "field_5cf7598bfd0dd",
                                    "operator": "!=",
                                    "value": "authorize"
                                }
                            ]
                        ],
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "",
                        "placeholder": ""
                    }
                ]
            }
        ],
        "location": [
            [
                {
                    "param": "block",
                    "operator": "==",
                    "value": "acf\/rei-payment-options"
                }
            ]
        ],
        "menu_order": 0,
        "position": "normal",
        "style": "default",
        "label_placement": "top",
        "instruction_placement": "label",
        "hide_on_screen": "",
        "active": 1,
        "description": "",
        "modified": 1562225680
    }
    

    and this is post content block

    
    {"id":"block_5c7f673b84e13","name":"acf/my-payment-options","data":{"field_5c36cf5487c08":{"row-0":{"field_5c36cf7387c09":"Cash Price","field_5c36cf8787c0a":"$8,999","field_5c36cf9f87c0b":"$12,999","field_5c6cc891d1407":"0","field_5c36cfae87c0c":"","field_5c36cfc287c0d":"Reserve Now","field_5cf7598bfd0dd":"authorize","field_5cf75a2dfd0de":"recurring payment","field_5cf75afcfd0df":"20","field_5d138724aaa98":"1","field_5cf75b5efd0e0":"days","field_5cf75be3fd0e2":"20"},"row-1":{"field_5c36cf7387c09":"Cash Price","field_5c36cf8787c0a":"$1234","field_5c36cf9f87c0b":"","field_5c6cc891d1407":"0","field_5c36cfae87c0c":"","field_5c36cfc287c0d":"Reserve Now","field_5cf7598bfd0dd":"authorize","field_5cf75a2dfd0de":"one time payment","field_5cf75afcfd0df":"2"}}},"align":"","mode":"preview"}
    
    • onion

    • January 30, 2020 at 7:19 pm

    is any solution like

    
    $block = new AcfBLock('block-name');
    $block-setAttr(['foo' => 'bar']);
    $block->getHtml();
    
  • I am answering the OP @stevekbristol

    Yes, if it is a flex field you can dynamically insert rows using PHP.

    I can’t give you specific details but I can explain what the flex field value will need to look like

    
    // the flex field is a nested array
    $flex_field_value = array(
      // each nested array represents a row of the repeater
      array(
        // the "acf_fc_layout" is a special value that tells acf the layout name
        'acf_fc_layout' => 'name_of_layout'
        // the remaining values of this nested array contains field_key => value pairs
        // for each field in the layout
        'field_XXXXXXX' => 'value for sub field',
        'field_YYYYYYY' => 'value for sub second field'
      )
    );
    

    each field value in each row will need a value that matches the way that ACF stores that value in the DB. In addition to this, if you have nested repeaters (repeater, flexible content, and group fields) these fields will also be arrays with values for each row that is similar to the flex field.

    Basically, this value needs to look identical to the value that is submitted by ACF fields when you click on the “update” button when editing a post.

    Once you have the flex field array populated you can use update_field() to populate the flex field… using the field key of the flex field

    
    update_field('field_ZZZZZZZZ', $flex_field_value, $post_id);
    
  • hmm

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.