Earlier, I’d written about how to create a category page in WordPress using the new and improved WordPress 6.0 Query Loop block. Unfortunately, this doesn’t work if you want to include all post types. The query loop block has an option on the right-hand side that allows you to choose either posts or pages for display. It doesn’t let you choose both. Here’s a screenshot:
By default, WordPress doesn’t allow you to add categories and tags to pages – only posts. A while back, I’d written an article on how add tags to WordPress pages. This is very useful, particularly if you’ve already created some pages and want to belatedly add tags to them. That article also included code to include both posts and pages in any tag query. Otherwise, even if you added tags to a page, they wouldn’t show up in the tag archives.
But the new Query Loop block has its own code and it can only show pages or posts. So while an ordinary archive page will show both post types, the Query Loop block from WordPress 6.0 will not. So we’re back to creating a page using an in-built template to get the results we want.
Since I use the Genesis theme on my website, I thought I’ll show you how to create an archive page using the “Blog” template on Genesis that only displays posts and pages from a single tag. You can modify this to show any category, or multiple tags, depending on the options you set using the “query_args” parameter as shown below.
Create a Page Using the “Blog” Template
Strangely, to create an archive page in Genesis, you should use the “Blog” template, and not the Archive template. The latter will output a sitemap-like list of titles, and doesn’t have the pagination you would expect from a category or a tag archive. So fire up a new page, and select the Blog template like this:
The Blog template has all the necessary tools to display a list of posts with featured images, excerpts, and “Next” and “Previous” pages as you would expect.
Crafting the “query_args” Parameter
By default, the blog template shows you a list of everything. But you can select what to show by properly constructing a “query_args” parameter. Here’s the full documentation for query_args, but it’s best illustrated with some examples:
To show only items tagged with “tech”:
tag=tech
To show items in tags 3 and 4:
tag__in= array( 3, 4 )
To show items in category 5:
cat=5
Exclude categories 7 and 8:
cat=-7,-8
Once you’ve figured out what kind of query_args parameter you want, scroll down to the bottom of the page editor till you come to a section labeled “Custom Fields”. Here, click “Enter new”, and in the “Name” field, type “query_args”, and in the “Value” field, type in the parameters you just determined. For example, I want to only show items tagged with “tech”. So my Custom Field screen looks like this:
Now save and publish your post. For me, this is the output:
As you can see, I have one page and one post tagged with “tech”, so both of these show up as expected. Note that this is only possible because I’ve implemented the code to add tags to pages, and got them to show up when tags are queried. If you want to do the same thing with categories, you need to modify the code accordingly.
Who knows, in the future, WordPress might expand the Query Loop Gutenberg block to allow us to include all kinds of post types, and not just either posts or pages. But until that happens, we have to continue using templates to achieve the results we want.
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply