How to add Categories and Tags to wp Pages

Historically, categories and tags have always been associated only with wp publications; they are one of the pillars of the classification and search of information in wp, and normally this approach is more than enough for most websites. However, when talking about medium and large size websites, the lack of this technique applied to standard wp pages is not a good scenario.

line markers

The pages, habitually static information not subject to a date (unlike posts), hardly ever need to be classified or searched… they have mostly been accessed through menus but what can we do if we need to add Categories, Tags or even custom taxonomies to Pages? Well, the solution is very simple, and you only need to add a few lines of code in the file funcions.php of your current WP theme.

Firstly, we need to add the support of categories and tags to the default wp posttype page. And normally, to define, work, add, or redefine any wp post type we need to do the changes using the action init. This is action wp uses by default to do this kind of operations so, we will simply need to call the function add_action() and also the function register_taxonomy_for_object_type() inside the function triggered by the action.

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

And secondly, we need to inform to wp that, from this moment, when it do a query over its database tables, it has to include wp pages when dealing with categories or tags. And this is very simple using the filter pre_get_post and forcing the introduction of the post type page into the list of the ‘classified by a taxonomy’ posttypes.

Basically, we check if the current wp query is driven by Categories or Tags and when any of these conditions is true then, we add ‘page’ to the post_types list used by the query engine.

function add_cats_and_tags_to_queries($wp_query) {
    if ( $wp_query->get('tag') || $wp_query->get('category_name') ) {
        $posttypes_list = $wp_query->get('post_type');
        if ( is_string ( $posttypes_list ) )  // we convert the string var in an array when it's necessary
           $posttypes_list[] = $posttypes_list;
        $posttypes_list[] = 'page'            // And here we add the additional type of post_Type, the 'page'.
        $wp_query->set('post_type', $posttypes_list );

// we could also add the word 'any' as a new WP_Query var, but it's always 
// better to control each one of post_types
// $wp_query->set('post_type', 'any');

add_action( 'pre_get_posts', 'add_cats_and_tags_to_queries');

And that’s all, any time you select to see a particular category, besides the rest of wp post types, the query will also include in its results the ‘pages’ that belong to this category.

By the way, if you have any doubt about how to register a category, tag or taxonomy for an existing post type in WordPress, please, take a look to the official documentation of the function register_taxonomy_for_object_type for further information and, on the other hand, if you need to introduce a more detailed control you could check out this official WordPress documentation about the WP_Query class.

Have a nice WordPressing!

4 Comments on How to add Categories and Tags to wp Pages

Leave a Comment

   Mandatory field
You can use these HTML tags inside the commment.
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>