Support » Developing with WordPress » Create post of CPT A in the post of CPT B through Gutenberg

  • Hi,

    I have two CPTs, ‘lesson’ and ‘game’. ‘Game’ only supports metadata, and I want to create ‘game’ in bulk when I create a ‘lesson’ CPT in Gutenberg through inserting custom block. I will save the meta data of ‘game’ in that custom block.

    The problem is how can I run function like wp_insert_post() to create ‘game’ only when I publish or update ‘lesson’? I guess render_callback for register_block_type() will run every time register_block_type(), and it will be a bad idea for me to save data into the database there, or am I wrong on this?

    Thank you for reading.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Why make ‘game’ a post type, when it is clearly not one? It is either a taxonomy or simply part of the content of ‘lesson’, especially if you are making it a block.

    @joyously Initially I thought storing the data of ‘game’ as a CPT would be a good idea, as there will be multiple ‘game’ per ‘lesson’. I am not sure if setting it as the meta of ‘lesson’ is a good idea.

    If setting it as post meta, how should I manage post meta with multiple levels?(First level being id of game, and second level being information like Black player, White player etc…). I want to be able to get the data of ‘game’ with get_post_meta() later on.

    I see.
    If you have ‘game’ as a CPT, won’t the data be in meta anyway? (just a different post’s meta)
    You can treat ‘game’ as separate, like an attachment is, so it has to exist separately before you reference it in your ‘lesson’.
    Or you can just leave the ‘game’ meta on the ‘lesson’, since each meta can have multiple values.

    Or don’t use meta at all. Just put the information into the content of the ‘game’ CPT. And treat it like an attachment, that pulls the content into the ‘lesson’ instead of just a link or having to read meta.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.