Skip to content
Advertisement

Custom Endpoint API based on attribute gutenberg block

Viewed 4 times

0

I need to return all posts that have the value of a specific attribute that is saved in the custom block attributes gutenberg. This value in the query will be according to the endpoint below.

http://idinheiro.local/wp-json/idinheiro/v1/blocks-posts/id-here-attribute-gutenberg-block

below my callback function. In short, how do I look for this attribute and put it there in get_posts?

Register routers

JavaScript

Response data

JavaScript

I thought of something that went something like this. Or if you have another solution on how to get this, I’ll be grateful

JavaScript

Advertisement

Answer

Based on your code provided, here is a potential solution that may solve your issue:

In your custom block cgb/block-idinheiro-blocks, you could set the value_attribute_my_block attribute source as meta if the block is only used once per a post/page (multiple can be restricted in the attributes – the default is true, set to false). NB. your meta field must be registered with register_post_meta() in php/your main plugin file or the value won’t save.

Storing the value as post_meta enables you to use the powerful meta query to retrieve posts into your function get_items() easily. This strategy avoids having to parse the content of the post, as by default on save block attributes are serialized. In your current query $args, your 's' parameter would find posts with the block but would still have to parse the value of the attribute, which would be an alternative but likely not as fast/performant as meta query would be.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement