-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support post metadata inside blocks #1842
Comments
➕ for this:
It's very similar to what I've brought up in #1754. |
A more general solution would be to allow the blocks to have persistent ids (opt in persistent ids), eg a block looks like something like this. Then if you have the post id somewhere like localised in the footer maybe, that's enough to do whatever you want with the content of the block — get it from postmeta, a custom table, an api etc.
|
This works now — you can specify attributes as being |
@mtias is there a link to the commit in which this happened or any resource describing in more detail your last comment? |
@Lewiscowles1986 You can find the documentation in the handbook: https://wordpress.org/gutenberg/handbook/block-api/attributes/#meta |
I am a late comer to this thread. I am trying my hand for the first time with metadata but not yet successful. Please help me understanding what I am doing wrong. Thanks! |
…rd-landscape Avoid fullscreen text entry UI in landscape mode
Issue Overview
With the current editor, the general practice (whether that's good or bad) is to use post metadata for anything custom that should be displayed in the post content. Most of this data can become information that is simply entered into a specific field of a specific block type. By parsing the blocks from the post content, it is also possible to retrieve such a specific piece of data, so we're good up to this point.
However, there's one thing that is currently not possible: Querying posts by such a specific piece of data. At the moment, since everything is stored in metadata anyway, we can use a
WP_Query
and itsmeta_query
functionality: For example, if we have a meta field for entering an email address (stored under a specific meta key), we can then query all posts where "[email protected]" has been provided for that field / key.Now with Gutenberg, if we have a block type (let's say we call it "Contact Data") where one can enter an email address, there's no way to properly query for all posts that have a specific email address in there, since that information is only available somewhere in the content. Of course we could just provide the email address we wanna query by as the search parameter, but that would simply search the entire post content, not only that specific type of email field in our "Contact Data" block).
Possible Solution
A possible solution for this could be to allow specific block types (at the code level) to register one of its "attribute field" to map to a specific post meta key. If the user enters data into that field, the value would then automatically be saved as metadata under that key (of course in addition to it being part of the post content, as usual). This would allow querying by such data.
It's important to highlight that registering such a meta key should only be used in cases where querying for it makes sense. Otherwise, if everyone registered all their attributes as metadata, the
postmeta
database table would become too cluttered.An alternative (that is possible now already) is to still have a metabox for the data somewhere, and only use the block type to reference that meta key. That would however require the block to have a PHP callback, furthermore I think it would be confusing to the user to enter that metadata somewhere and then also having to insert a block that displays that data. Given the fact that Gutenberg demotes meta boxes, that alternative would be even worse. So I think my above suggestion of mapping specific block type fields to specific post meta keys is much nicer.
I hope that explanation is clear enough, as it's a somewhat complex topic. If not, please ask, I'm happy to clarify.
The text was updated successfully, but these errors were encountered: