Determine if a specific Gutenberg block is used on pages/posts

As far as I am aware, there is no built-in way to get all the posts in a site that contain a specific block.

Two potential things you could do:

  1. You can use the has_block() function to determine if a specific post has a specific block. (there is also a more accurate parse_blocks() function available). You could loop over all content and fine posts that contain the block that way (probably not great performance wise).
  2. Add a custom post meta filed that you toggle to true when a user adds the block to the post and false when the user removes the block. Then you can use a custom WP_Query to get posts where the meta value is true. You can use the useSelect() React hook to get all the blocks on each post, and check if any have the block you’re looking for. Then use a useEffect() hook to update the meta.

error code: 523