Support » Developing with WordPress » Create multiple Gutenberg meta blocks for the same meta key

  • Resolved kghugo2000

    (@kghugo2000)


    Hi,

    Is it possible to create multiple Gutenberg meta block for the same meta key, mimicking the result of using add_post_meta() with the same meta key, which creates multiple entries for the same meta key?

    Currently I have create a meta block following the tutorial from the Block editor handbook, but when I create multiple of that meta block and try to edit them, their value stay the same. I believe it is because they are using the same attributes.

    How can I tackle this issue?

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

    (@bcworkz)

    The tutorial registers its post meta with 'single' => true,. If that is set to false instead, added values will be pushed into an array. That does not help an individual block to know which value is its own value. Each block in a single post would need a unique identifier which it could use to access its own value.

    The identifier and its value could be saved as a JSON string. Retrieving the right value would involve stepping through all saved values under the one key and checking if its identifier is within the JSON string. See this section of the Handbook.

    Thread Starter kghugo2000

    (@kghugo2000)

    Thank you so much for your advice. If I save it as a JSON string, what hook should I use to deserialize the JSON string on the server? I guess render_callback is not used for this.

    For the unique identifier, I guess I can use the number of the order of block as the unique identifier, but how can I get it?

    Moderator bcworkz

    (@bcworkz)

    To show the saved value in the block itself, extracting the right value would be part of the edit: callback for registerBlockType(). Anywhere else the meta value is used, the hook used depends on the context in which the value is needed.

    As for the ID, I’m not sure what would work, sorry. This is as new a concept for me as it is for you. Maybe use the current time stamp? Store it as a normal block attribute so the block knows which one belongs to itself? Code external to the block could parse the timestamp IDs out of post content. Such a process would also reveal which order the blocks occur in. Both order in content and order of insertion could be discerned.

    The ability to store multiple values per post under one meta key has existed for a very long time, yet I’ve never knowingly encountered it being used. Out of curiosity, if I may ask, in what way are you using multiple meta values per post under one key?

    Thread Starter kghugo2000

    (@kghugo2000)

    Thank you so much for your advice. They gave me a new insight to this issue.

    The problem I want to solve is this: I ran chess class and I want to store data of a lesson and all games played in that lesson into database. I have set up a CPT called 'lesson'.

    I know I can set up 2 CPT, ‘lesson’ and ‘game’, save them separately and reference them as an one-to-many relationship, but I want to save multiple ‘game’ quickly(Because I have to save it during the class). It will be much slower to add 10 ‘game’ than adding 10 blocks that saves metadata of ‘game’ in the block editor.

    Therefore, I am try to explore the possibility for me to save data of multiple ‘game’ as the metadata of ‘lesson’.

    I am think of saving it as an array, containing all the data of the game, like who is Black player, who is White player etc…I want to get these metadata like with function likeget_post_meta().

    What is the most professional and effective way to tackle this issue?

    Moderator bcworkz

    (@bcworkz)

    I’ve always marveled on how compactly chess games can be recorded.

    As I read your description I started thinking “That’s a problem easily solved by a simple array.” Only to read on to find you’ve arrived at the same conclusion 🙂

    I think that sort of data makes more sense as a traditional meta box instead of utilizing meta blocks. But you could still use a single meta block if you wanted to. The difference being all games are recorded in expandable fields within the one meta block. I’m more familiar with traditional meta boxes, so I’ll focus on that, but AFAIK there’s no reason an editor block couldn’t do the same thing.

    It starts out with a single field for one game. More fields can be added to the same box. When the post is saved, if field names are similar to name="game[]" (square brackets are key), the data will already be in array format. Data still needs to be validated and sanitized. The array can then be saved directly into a single post meta record.

    I believe the Advanced Custom Fields or Gravity Forms plugins allow expandable fields to be utilized. They’ll handle all the nitty gritty details. You only need to define the field for posts and fill in data, then the plugin handles the rest.

    Thread Starter kghugo2000

    (@kghugo2000)

    Thank you so much for your insight. The last thing I am uncertain is where should I store those metadata. I am not experienced enough and I guess I have to consult your opinion again(to avoid doing whole thing again in the future).

    Option 1: Saving data of games as metadata of ‘lesson’
    Option 2: Saving data of a game as metadata of ‘game’, and create one-to-many relationship with ‘lesson’.

    Would the relationship between CPT matters in certain situation? If so, what are those situations that come to your mind?

    If not, I guess both options will work for me and I explore them.

    Thank you so much for your help.

    Moderator bcworkz

    (@bcworkz)

    It doesn’t really matter from a technical standpoint. It all ends up in the metadata table no matter what. (well, custom tables are possible, but are probably not warranted in this case). A single game saved with its CPT would be easier to implement because then you don’t need expandable fields. But then relating games to lessons gets a bit messy because WP doesn’t normally relate between different CPTs. There are exceptions, such as attachment to post, so it’s not like it’s not possible.

    For the sake of exploring options, what if there were just one hierarchical CPT called “chess”? Lessons would all be top level posts. Each lesson would have multiple children for every game in the lesson. WP doesn’t care what you call these things, the labels are for our human convenience. This way, WP handles the relationship automatically. Game permalinks automatically include the top level lesson name with the game name. You’ll have to mentally know that lessons are top level and games are children. That’s the only drawback I can think of. How each post is named ought to make the distinction clear.

    One other consideration would be if meta data would be searched for directly. In the case of a string of chess game moves, I don’t imagine that being important. Searching for meta data saved as arrays could get pretty messy.

    Thread Starter kghugo2000

    (@kghugo2000)

    Thank you so much for your advices! In the end I used carbon fields instead of ACF, and build repeatable fields for CPT ‘lesson’ to store data I want. Let me share with you me work later when I complete it:). Thank you!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome! Your willingness to share the result is appreciated.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Create multiple Gutenberg meta blocks for the same meta key’ is closed to new replies.