Safely enable tags for pages
Tags aren’t available for pages. Safely enable them using this PHP snippet to be added to your functions.php file. If there’s already a snippet in place and you’re unsure of its content, this safe snippet won’t throw errors. It checks if the tags are already enabled or not.
// This function checks if the 'post_tag' taxonomy and 'page' post type exist.
// If they do, it registers the 'post_tag' taxonomy for the 'page' post type.
function tags_support_all() {
// Check if 'post_tag' taxonomy and 'page' post type exist
if (!taxonomy_exists('post_tag') || post_type_exists('page')) {
// If they do, return early and do not register the taxonomy
return;
}
// If they don't, register 'post_tag' taxonomy for 'page' post type
register_taxonomy_for_object_type('post_tag', 'page');
}
// This function ensures all tags are included in queries
function tags_support_query($wp_query) {
// If a tag is queried, include all post types in the query
if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
}
// Add the 'tags_support_all' function to the 'init' action hook
add_action('init', 'tags_support_all');
// Add the 'tags_support_query' function to the 'pre_get_posts' action hook
add_action('pre_get_posts', 'tags_support_query');
The provided PHP code snippet should be compatible with PHP versions 7.4 through 8.3. The functions used (register_taxonomy_for_object_type, post_type_exists, taxonomy_exists, add_action) are part of the WordPress core and have been stable for many years.