Custom Endpoint API based on attribute gutenberg block

You don’t. That’s not how block attributes work, and it’s not what they’re for.

A blocks attributes belong to that block, and are used by the editor to generate that blocks HTML, but once they’re saved in the database, they’re just HTML. There’s no block table, no mapping/index of block attributes in the database, post content is just post content.

For this reason, querying posts by the attributes on their blocks is equivalent to querying posts by the parameters on their shortcodes, or the attributes on a hyperlink. It doesn’t make sense, it’s not what they were intended for, you’ve made a mistake.

Instead, if you want to query for posts with X or Y or Z, you need a taxonomy. Post meta works too but has major performance costs. Block attributes don’t.


The best you can do, the closest you can get, is to fetch all posts, load them all into memory at the same time, parse their post content into blocks, process those blocks to find the block type you’re looking for, check it’s attributes, and add it to a list.

This will be very slow, limit the number of concurrent visitors to less than a handful, and will cap the maximum number of posts on your site to less than 100. Any more and major performance and scaling issues will occur, you run the risk of out of memory errors, or out of time errors.