Save meta values in database
- This topic has 12 replies, 4 voices, and was last updated 5 years, 4 months ago by
Przemek.
-
AuthorPosts
-
October 14, 2019 at 3:17 PM #16511
closemarketing
ParticipantHow can I save the meta values from the gutenberg block as a meta value?
Is it possible? That would be very nice, becouse you can use it to filter a post type or similar.
Thanks.
October 14, 2019 at 5:23 PM #16512closemarketing
ParticipantOr, Could I use meta values in template block?
October 15, 2019 at 4:43 PM #16530Anh Tran
KeymasterHi,
Unfortunately, blocks can save data only to its own. The post meta allows only 1 pair of key-value, but you can add many blocks in a post. So, if that's possible, that might be confusing.
In order to use meta values in a block template, you can simple register meta fields normally with Meta Box, and use helper functions
rwmb_meta
to get them to show.October 15, 2019 at 5:22 PM #16531closemarketing
ParticipantSo, I can use rwmb_meta in template block?
October 15, 2019 at 5:23 PM #16532closemarketing
ParticipantWould you add in your roadmap to save meta values as well?
October 15, 2019 at 5:47 PM #16536Anh Tran
KeymasterSo, I can use rwmb_meta in template block?
Yes, you can.
Would you add in your roadmap to save meta values as well?
As I explain in the previous reply, this doesn't work because you can add multiple instances same block. If one field in each blog has different values, then there's no way to decide which value will be save in the post meta.
October 15, 2019 at 6:18 PM #16537closemarketing
ParticipantHow can i use rwmb_meta? Could you give me an example?
October 15, 2019 at 6:40 PM #16538closemarketing
ParticipantWhat are the limitations for template.php? I'm trying to use rwmeta and WordPress Functions like the_excerpt, the_post_thumbnail, etc. But they does not work.
October 18, 2019 at 10:32 AM #16578Anh Tran
KeymasterHi, please see this video: https://www.loom.com/share/80fa3ff5dc6149a9bd2cb640a62aa69c. I demonstrate how to use
rwmb_meta
,rwmb_the_value
in the block render callback.Just a note: when you use these helper functions, don't forget to use the parameter
$post_id
. If you want to use other WordPress functions, remember to use it as well, or you'll get the wrong info when preview the block in the edit screen.October 18, 2019 at 4:22 PM #16596closemarketing
ParticipantNice! that's all that I need!
Congratulations for the plugin.
December 20, 2019 at 3:16 AM #17594Przemek
ParticipantIf one field in each blog has different values, then there’s no way to decide which value will be save in the post meta.
Anh, I understand your point, but the fact is that blocks are created by aware developers and Gutenberg itself gives the possibility of storing data in post meta. (For example, it is enough to allow one such block per post and potential confusion is over)
I think there are many use cases for this.
An instant example from me five minutes ago: I want to create a block including editable featured image and it would be great if this advice would work in MB Blocks: https://support.metabox.io/topic/adding-fields-to-featured-image-metabox/December 20, 2019 at 4:43 AM #17596Content Pilot
ParticipantI second this. If
multiple
is set tofalse
then it could work in tandem with another flag likepost_meta
set totrue
so that the block can save to the db as the block's ID for themeta_key
Not totally sure if it is possible but saving to the db as post_meta and rendering on the frontend at the same time would be a game changer. I need blocks for page creating but still use pieces of data on archive pages or search result like phone numbers, emails, etc.
December 20, 2019 at 4:45 PM #17602Przemek
ParticipantI'm not familiar with MB Block internals, but in pure Gutenberg there are just 2 steps: registering post meta in PHP and defining "source: 'meta', meta: 'meta_field_key'" in JS block declaration. I feel that it might be not a big leap in MB code.
-
AuthorPosts
- You must be logged in to reply to this topic.