Dimmuborgir / Iceland

Simple Block Type Search Plugin

One can e.g. use the general search in the /wp-admin backend to find some text within the post’s content that stores the blocks:

We can search for a prefixed block name wp:my-plugin/my-block within the posts:


where we have an extra space (url encoded as %20and / as %2F) after the block’s name, as the first part of it would be stored as <!-- wp:my-plugin/my-block --> or with attributes <!-- wp:my-plugin/my-block {...} --> in the post’s content.

The front-end search works as well, but without the post type filtering as in the backend (unless adjusted with a plugin):


This block search might not be 100% accurate (e.g. if you blog about block names) but it might give you a good enough estimate in most cases.

More accurate searching

We note that searching for <!-- wp:my-plugin/my-block  with:


will be split up in two words in the generated SQL:

AND (((wp_posts.post_title LIKE '%<!--%')
OR (wp_posts.post_excerpt LIKE '%<!--%')
OR (wp_posts.post_content LIKE '%<!--%'))
AND ((wp_posts.post_title LIKE '%wp:my-plugin/my-block %')
OR (wp_posts.post_excerpt LIKE '%wp:my-plugin/my-block %')
OR (wp_posts.post_content LIKE '%wp:my-plugin/my-block %')))

but we can avoid it with the sentence parameter:


and get a search query like:

AND (((wp_posts.post_title LIKE '%<!-- wp:my-plugin/my-block %')
OR (wp_posts.post_excerpt LIKE '%<!-- wp:my-plugin/my-block %')
OR (wp_posts.post_content LIKE '%<!-- wp:my-plugin/my-block %')))

With code

Alternatively we could use a custom WP_Query search like:

WP_Query( array( 
    's'         => '<!-- wp:my-plugin/my-block ', 
    'sentence'  => 1, 
    'post_type' => array( 'post', 'page' ),
) );

or run has_block( 'my-plugin/my-block', $post_content ) on all the relevant post content for the given block name or use parse_blocks( $post_content ) if you need more info from it.

Reusable blocks

We should note that reusable blocks are stored like:

<!-- wp:block {"ref":23495} /-->

in the post’s content, so we might look into the wp_block post type that stores it:


Simple Block Type Search Plugin

To make things easier to locate the posts that contain a given block type, I’ve created a plugin that adds a dropdown filter for registered block types, under each list table in the admin:

to simplify the search that is automatically added when filtering.

The selection contains all registered block types (including core and custom ones):

Create a plugin file and activate the plugin:

/* Plugin Name: Simple Block Type Search
 * Author:      birgire
 * Plugin URI:  https://xlino.com/simple-block-type-search-plugin/ 

// Display a filter dropdown for registered block types in admin list tables
// for post types that support the block editor.
add_action( 'restrict_manage_posts', static function ( $post_type ) {
	if ( ! use_block_editor_for_post_type ( $post_type ) ) { return; }
	$block_types = \WP_Block_Type_Registry::get_instance()->get_all_registered();
	$options = array_reduce( $block_types, function( $options, $block_type ) {
		$val = '<!-- wp:' . str_replace( 'core/', '', $block_type->name ) . ' ';
		return $options . sprintf ( '<option value="%s" %s>%s</option>',
			esc_attr( $val ),
			selected( get_query_var( 's' ), $val, false ),
			esc_html( $block_type->name )
	}, '' );
	printf ( '<select name="s"><option value="">%s</option>%s</select>', 
		 esc_html__( 'All Block Types', 'simple_block_type_search' ),
} );

// Set a '<!-- wp:' block search as a full sentence search.
add_action( 'pre_get_posts', static function( \WP_Query $q ) {
	if ( is_admin() && $q->is_main_query() && $q->is_search() ) {
		if ( strpos( $q->get( 's' ), '<!-- wp:' ) !== false ) {
			$q->set( 'sentence', 1 );
} );

To get all registered block types, we used:


and to only display the dropdown on supported post types we used:

Finally we set sentence as 1 when the search string starts with <!– wp:

Here we used the built-in search feature and the user interface of the admin backend.

This started as an answer on WordPress StackExchange:


Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *