• checkpoint71

    (@checkpoint71)


    i have created a custom post type ‘page’; then create a page with wp_insert_post;

    What i want to do now is to init gutemberg blocks for this page; if i do a template at register it only works if the page is manually made…but how to do this by wp_insert_post?

    What i would like to do is a custom post type as page, giving the page gutenberg blocks per programm (would be nice if i could fill the blocks with data from db)…so that you can see and edit the blocks…is that possible? Doing it with page-template is not the target, because there is no edit then, right?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    Post type ‘page’ is reserved by WP, you must not attempt to make a CPT with the same name. Anything following slug rules but ‘page’ and ‘post’ would be acceptable.

    If you want to pre-set post content with blocks, enter your desired content into a normal post block editor using the blocks you want. View what you have in “code” view (through the 3 dot menu at upper right). This includes the proper HTML comments that make blocks blocks. Use this code view content as the string to set for content when you insert via code.

    Thread Starter checkpoint71

    (@checkpoint71)

    i mean the capability_type ‘page’ not the name of CPT;

    i tryed it with simple <p> at post_content and it runs…so i will try the rest tomorrow, thanks.

    Thread Starter checkpoint71

    (@checkpoint71)

    one more question: if i feed blocks in this way and give the blocks content from a database, how can i do an update, when the data chanced?

    With wp_update_post i only can change complete content, right?

    For Example: I do a <p> block with text filled from database; then i addes manually one more block; is it possible only update the <p> block if data changed? Or is the manually block gone then?

    Moderator bcworkz

    (@bcworkz)

    Yes, when you call wp_update_post(), the content provided completely replaces any previous content. If you want to append or prepend new content to existing, first fetch the existing content, add your new content, then use wp_update_post(). Inserting or removing within the existing content gets trickier.

    If the content you are inserting is coming from a DB somewhere, is there any chance of WP connecting to that DB directly, querying for content, then displaying it on a particular page? This is a possibility on the WP end. Other DBs may or may not allow external requests.

    Thread Starter checkpoint71

    (@checkpoint71)

    The content is imported via XML and written to WordPress-Database; So connection to content is no problem. I know how to do this via page-template, but how to do with a single block?

    The first problem is at wp_insert_post i give him blocks with post_content; If i understand you i give here HTML as content. But on the blocks i filled with data from database i should notice there something like block-ID, so i am able to update only this block later…

    The second problem would be later to update only this block of the complete content if data is chanced…

    maybe the solution can be with reusable blocks? Bad would be there that i would need one block for every data in the database, but can reusable blocks be genereated and updated per Code?

    Or do you have any idea for a good solution?

    Moderator bcworkz

    (@bcworkz)

    I’m behind the times with block coding, so I still look to shortcodes for such things. You can of course still use shortcodes or develop a true block equivalent as you prefer. My thought in shortcode terms is to pass whatever data is necessary as attributes to query directly for the desired remote content. Every time the page is requested the shortcode directly queries for the remote content and returns the pertinent content for output that replaces the shortcode.

    Since it comes from the source, there is no concern about syncing data locally (though there is merit in short term caching for heavily used data). You avoid maintaining redundant data and the entire syncing issue by going directly to the source for data.

    Thread Starter checkpoint71

    (@checkpoint71)

    OK – but shortcode content is only seen at htmlpage then, but not for the user edit the page, right? So i have to look for block developement. Is there any example for block developement with data connection?

    Moderator bcworkz

    (@bcworkz)

    Not exactly. You would have a dynamic, server-side rendered block as described here. The server-side code would make the DB connection, fetch appropriate data, then parse it into content which is sent to the client for placement within the block. The wpdb class (not global $wpdb object though) could be used to make the DB connection, or you can use PHP’s mysqli_*() functions. Essentially an Ajax technique within a block.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘wp_insert_post with custom post type page and init’ is closed to new replies.