Extend the Query Loop Block using a low-code workaround

I have been trying to figure out how to use the very handy Query Loop Block to display only a few specific pages. The Query Loop Block works to show posts assigned to a specific Category or Tag.

The Query Loop Block can show all pages, but there are no filters available to show specific individual Pages or groups of Pages. I have not been successful with the keyword filter to show specific Pages in the Query Loop Block.

I realized I was overlooking an available low-code path to accomplish my goal. I had an idea that could serve as a workaround until the Query Loop Block had a few more options for filtering by IDs of Posts, Pages. I could use one of WordPress’ built-in taxonomies for my pages.

When you look at your WordPress dashboard you will see displayed in the Posts area “Categories” and “Tags“. Categories and Tags are built-in WordPress Taxonomies. Pages do not naturally use taxonomies as Posts do. However, you can modify your website to connect a taxonomy to your Pages.

To allow WordPress’ built-in Categories and Tags to work with your Pages – first install and activate the plugin Code Snippets. Save the following code as a new snippet. Activate the snippet. Check in your WordPress Dashboard Pages area to confirm that Categories and Tags are now available.

Add this code:

add_action( 'init', 'shellc_add_categories_and_post_tags_to_pages' );
function shellc_add_categories_and_post_tags_to_pages() {
register_taxonomy_for_object_type( 'post_tag', 'page' );
register_taxonomy_for_object_type( 'category', 'page' );

Set up a new, unique Category or Tag that you want to assign to specific Pages. When you set up a new Query Loop Block and select Pages as the post type, the filter for categories and tags will be available for you to use to configure the Query Loop Block display.

  • Notes:
  • Works on version WordPress 6.0 or later
  • Create a full backup before trying out the suggested code
  • Tested using Twenty Twenty One Theme