Support

Account

Home Forums Gutenberg wp_insert_post() and add acf blocks to the page

Helping

wp_insert_post() and add acf blocks to the page

  • Hi,

    I would like to create a new page with wp_insert_post() -function and add some of my own acf blocks with content to the page. However I am only able to add default WP blocks to the page, but not acf blocks. Any tips on how to add acf blocks with content to the page?

    Code that works (default WP blocks only):
    wp_insert_post(
    array(
    ‘post_title’ => ‘Test Post’,
    ‘post_type’ => ‘page’,

    ‘post_content’ => ‘
    <!– wp:heading {“placeholder”:”Post Heading”} –>
    <h2>Here is the title</h2>
    <!– /wp:heading –>

    <!– wp:paragraph {“placeholder”:”Post Paragraph”} –>
    <p>here is some text</p>
    <!– /wp:paragraph –>’,

    ‘post_status’ => ‘publish’,
    ‘post_author’ => 1,
    )
    );

    What I am trying to do but not able to(add acf blocks):
    wp_insert_post(
    array(
    ‘post_title’ => ‘Test Post’,
    ‘post_type’ => ‘page’,

    ‘post_content’ => ‘

    <!– acf:my-example-block –>
    <!– /acf:my-example-block –>’,

    ‘post_status’ => ‘publish’,
    ‘post_author’ => 1,
    )
    );

    Furthermore, I would like to add some content to the blocks like:
    wp_insert_post(
    array(
    ‘post_title’ => ‘Test Post’,
    ‘post_type’ => ‘page’,

    ‘post_content’ => ‘

    <!– acf:my-example-block {“data”:[“content”:”here is the content to the acf field called content”]} –>
    <!– /acf:my-example-block –>’,

    ‘post_status’ => ‘publish’,
    ‘post_author’ => 1,
    )
    );

    Thanks for any help in advance!

  • I was able to add a block WITHOUT content. The issue was that instead of “acf:my-example-block” I needed to write “wp:acf/my-example-block”. However I am not able to put any content to the block generated. The content needs to go to the acf fields of the block. Any tips on this?

    I have tried this without success:
    <!– wp:acf/my-example-block {“data”:{“content”:”here is some content to the acf field”}}–>
    <!– /wp:acf/tt-block-content –>’

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

You must be logged in to reply to this topic.