How to add Tags to Pages

Is it possible to operate tags with WordPress pages in the same way as posts do?

While by default WordPress posts support the use of tags the pages don’t.

I didn’t wish to use a plugin. I like to keep the number of installed plugins to a minimum. If its possible to implement the tags on the pages with minimal code added to the functions.php file that would be fine.

I found the WordPress codex reference to register_taxonomy_for_object_type.

Thus to add category and tags to pages there are these two references

<?php register_taxonomy_for_object_type( 'category', 'page' ); ?>

and

<?php register_taxonomy_for_object_type( 'tags', 'page' ); ?>

I chose to join the two together within one function, as shown below:

add_action('plugins_loaded','add_taxonomies_to_pages');
function add_taxonomies_to_pages(){
    register_taxonomy_for_object_type('category', 'page');
    register_taxonomy_for_object_type('post_tag','page);
}

Utilising register taxonomy the suggestion appears to be to reference against init not plugins_loaded

The above covers the addition of tags to the admin view of editing pages. Next step is to handle the filtering of pages according to their categories and tags.

Add below to source pages with relevant categories and tags for public viewing:

// ensure all tags and categories are included in queries
function tags_categories_support_query($wp_query) {
  if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
  if ($wp_query->get('category_name')) $wp_query->set('post_type', 'any');
}

// tag and category hooks
add_action('pre_get_posts', 'tags_categories_support_query');

With the addition of a short function and appropriate action call it’s possible to add tags to WordPress pages allowing them to operate just as posts do by default.