WordPress Gutenberg, update post content programmatically

I have been starting to testing out Gutenberg editor with both ACF and custom blocks. And I have been looking around to solve my problem but I couldn’t find anything about this topic (Maybe my google skills is not good enought)

But my case is this:
I have a custom post type where I want to set a template so they can’t move around the blocks and add other blocks and so on. And on this post type around 70% is created by code. Because it is fetching all the information from an API.

Test 1: I have created an ACF block with all the fields I need, and it is working as it should when I create a new post from WP admin. But when I run the update_field function it is saving it to post_meta table as it did before. So my question here is how do I update a field so it saves it to post_content and not to post_meta table.

Test 2: I created custom blocks for all of the fields (convert each ACF field to and block) and set up the template to use these blocks only.
But here I have no idea how update update post_content with PHP or Javascript.

I hope you can help me out with this 🙂 If anything is unclear tell, and I will try to explain it

1 Answer
1

But my case is this: I have a custom post type where I want to set a
template so they can’t move around the blocks and add other blocks and
so on. And on this post type around 70% is created by code. Because it
is fetching all the information from an API.

This can be done by saying so when registering the post type. You can list the blocks that are allowed, aren’t allowed, initial starting blocks, wether they can be deleted/added/moved, wether new blocks can be added, etc

Test 1: I have created an ACF block with all the fields I need, and it
is working as it should when I create a new post from WP admin. But
when I run the update_field function it is saving it to post_meta
table as it did before. So my question here is how do I update a field
so it saves it to post_content and not to post_meta table.

The answer is you don’t. You didn’t in the classic editor, why would Gutenberg be any different

Test 2: I created custom blocks for all of the fields (convert each
ACF field to and block) and set up the template to use these blocks
only. But here I have no idea how update update post_content with PHP
or Javascript.

ACF blocks aren’t post content, they’re blocks yes, but they don’t store anything in post content. I’d call them “meta blocks”, that serve purely as UI in the editor.


So, what you’re looking for are gutenberg templates ( it’s really important that you read the handbook, it will save you a lot of time )

https://wordpress.org/gutenberg/handbook/templates/

A block template is defined as a list of block items. Such blocks can have predefined attributes, placeholder content, and be static or dynamic. Block templates allow to specify a default initial state for an editor session.

It then provides an example of registering a custom post type:

function myplugin_register_book_post_type() {
    $args = array(
        'public' => true,
        'label'  => 'Books',
        'show_in_rest' => true,
        'template' => array(
            array( 'core/image', array(
                'align' => 'left',
            ) ),
            array( 'core/heading', array(
                'placeholder' => 'Add Author...',
            ) ),
            array( 'core/paragraph', array(
                'placeholder' => 'Add Description...',
            ) ),
        ),
    );
    register_post_type( 'book', $args );
}
add_action( 'init', 'myplugin_register_book_post_type' );

As well as template locking:

https://wordpress.org/gutenberg/handbook/templates/#locking

Sometimes the intention might be to lock the template on the UI so that the blocks presented cannot be manipulated. This is achieved with a template_lock property.

Providing a new parameter when calling register_post_type:

'template_lock' => 'all', // or 'insert' to allow moving

As well as other examples. For adding ACF blocks to these templates, you will need to consult ACF support as ACF is a 3rd party plugin, and 3rd party plugins are off topic

Leave a Comment