WordPress’s taxonomy can be pretty confusing. First, we have the default separation between “posts” and “pages”. Then we have categories and tags. In addition, we can have custom post types which opens the door to an endless number of variations.
Figuring out the interactions between the taxonomies can be tricky. For example, by default, “pages” on WordPress cannot be assigned categories or tags. The necessary slot for assigning them is simply missing as shown here:
In this article, I will show you two things:
- How to Assign tags to Pages as well as Posts
- How to make Pages show up in the tag archives
Step 1: Ensuring that We Can Assign Tags to Pages
This fix is a relatively simple one. Open up your functions.php or custom PHP plugin and paste the following code at the bottom. In case you don’t know how to do this, read my comprehensive guide on adding code snippets to WordPress.
// add tag support to pages function tags_support_all() { register_taxonomy_for_object_type('post_tag', 'page'); } add_action('init', 'tags_support_all');
This simple snippet uses the “register_taxonomy_for_object” function to include “page” in the “post_tag” taxonomy. Once you’ve saved and verified your changes, you’ll see a new widget on the right-hand side of the WordPress editor when you create or edit a page:
That’s all there is to it! Now you can assign tags to pages. But our work isn’t done.
Step 2: Display Pages in the WordPress Tag Archives
It sounds weird, but the default tag archive doesn’t display all post types. So even though we’ve configured pages to have “tags” assigned to them, they won’t actually display in any tag archive. For example, here’s a screenshot of the “tag” archive on my test site:
This archive for the “tech” tag is missing the page on which I just added a new tag of “tech”. To fix this problem, we need to configure WordPress so that the query is modified when requesting a tag.
As before, enter the following code in your WordPress installation:
// ensure all pages are included in tag queries function tags_support_query($wp_query) { if ($wp_query->get('tag')) $wp_query->set('post_type', 'any'); } add_action('pre_get_posts', 'tags_support_query');
The “pre_get_posts” filter is called whenever WordPress is about to make a call for posts. If it detects that we’re requesting a tag page, it alters the query so that the “post_type” is set to “any”. That includes pages, and any other custom post types that might have tags assigned to them.
Once this code is saved, our tag archive now displays all the posts and pages with a particular tag as it should like this:
Tying it All Together
So now that we’ve done this, let’s say we want to display a list of posts and pages on the sidebar that shares the same tags as the current post. I personally use a plugin called “List Category Posts”. You can download and install it from the WordPress plugin repository here.
While the name suggests that it only displays a list of posts based on category, there is an attribute that allows it to read the tags as well. Here’s the shortcode snippet that I use on my own sidebar:
[catlist currenttags="yes" post_type="page,post" numberposts=5 excludeposts=this]
As you can see, I explicitly need to include the “page” post type in the shortcode for it to trigger a list of pages along with the posts. The “currenttags” attribute gets a list of tags from the displayed pages and uses them to create a query. I also exclude the existing post for obvious reasons.
All in all, this is a comprehensive solution to displaying a list of posts and pages on your sidebar based on a current tag. The code snippets in this article are crucial to enabling this functionality.
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!
gwen says
Hello, thank you for this article which allowed me to remove a plugin to add tags in the pages. Now I would like to know how to display the tags in the pages (they do not display)? Have you made an article about it? Thanks again and congratulations for your work
Alex says
Thank you for this article, it was key to helping me include tagged pages in archives and other tag related queries usually reserved only for posts.