Currently, we cannot add tags and categories to our WordPress pages.
Why add tags to pages?
Tags are very useful. They can be applied in a variety of non-standard, but powerful ways to enhance our blogs.
I use tags to filter out file loads and certain types of plugin operations. For example, I only run the wp-syntax plugin functions on pages that actually contain code. Similarly, we can use tags to load in special galleries for particular pages, or restrict a given plugin to a specified group of posts.
This allows us to optimize our WordPress blog and increase page speed.
Tags and categories are also used by plugins to identify related posts (for example the YARPP plugin). Adding tags to pages will presumably help the plugin return better results for both related posts and pages.
How to Add Tags and Categories to WordPress Pages
We can add tags to our WordPress pages by simply using the add_meta_box command as follows –
// Add to the admin_init hook of your theme functions.php file add_meta_box( 'tagsdiv-post_tag', __('Page Tags'), 'post_tags_meta_box', 'page', 'side', 'low');
Argument 1 must be set to tagsdiv-post_tag because this name is used in other WordPress functions to ensure that the metabox operates correctly and the proper tag values get saved with the page.
Argument 3 must be set to post_tags_meta_box because this is the native WordPress function used to render the tag metabox.
Similarly, we can use the same add_meta_box command to add a category metabox to our WordPress page.
add_meta_box( 'categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'core');
Saving Tags and Categories for WordPress Pages
You will quickly notice that when you update your WordPress page, the category does not get saved.
This is because the WordPress system does a check with the is_object_in_taxonomy function to see if categories can actually be assigned to pages.
To properly save our tag and category data, we want to properly register our WordPress pages to the post_tag and category taxonomies. We can do this with the register_taxonomy_for_object_type function.
// Add to the admin_init hook of your theme functions.php file // Add tag metabox to page add_meta_box( 'tagsdiv-post_tag', __('Page Tags'), 'post_tags_meta_box', 'page', 'side', 'low'); register_taxonomy_for_object_type('post_tag', 'page'); // Add category metabox to page add_meta_box( 'categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'core'); register_taxonomy_for_object_type('category', 'page');
Success! Once we do this, our categories get saved with our pages.
Note – As pointed out by XYDAC metaboxes automatically get added as part of the register_taxonomy_for_object_type function. Therefore, we can streamline our code and remove the add_meta_box commands.
// Add to the admin_init hook of your theme functions.php file register_taxonomy_for_object_type('post_tag', 'page'); register_taxonomy_for_object_type('category', 'page');
How to Retrieve Both Posts and Pages for Tag and Category Links
Now that we have set up tags and categories for WordPress pages, we may want our tag and category blog links to also retrieve those pages.
To do this, we can hook into the WordPress request filter.
// Add to the init hook of your theme functions.php file add_filter('request', 'my_expanded_request'); function my_expanded_request($q) { if (isset($q['tag']) || isset($q['category_name'])) $q['post_type'] = array('post', 'page'); return $q; }
We Are Done!
Enjoy your new expanded WordPress pages.
Sougata says
Hi
after adding the code, on the Category panel taking one Category at a time… couldn’t able to select multiple category for any single page.
Please suggest.
thanks
Sougata
rasel says
How to show this categories on front end.
AFril says
Thanks! This saves my life ^_^
Pramesh Pudasaini says
I want to remove tags and categories from the home page. How do I do it?
Joe says
Hey thanks Shiba for your great article. Anyways that was not what I am looking for. Can you tell me maybe how wordpress saves tag to post relations in the databse?
Thanks 🙂
ashleigh says
For some reason this code is causing my wp editor & page to be a white screen….any reason why it is doing this? Thanks for the help.
ShibaShake says
Likely, there was a syntax error, which can come from missing characters, adding the code in the wrong place, etc. When there is a hard error, the code stops, so the page does not get loaded. On my test site, I turn on WP debugging so that I can better track down the problem.
http://codex.wordpress.org/Debugging_in_WordPress
Markee says
thx, works like a charm for me!
Aashutosh Kumar says
Fine, But My problem is i have added
Here every thing is working fine, But the tag count displaying at admin side post_tag page is incorrect it showing like 0 but on clicking on the count i have found some post which is not considered in count.
Could anybody help me to get out of this, i am getting tired while doing this. i will be very thankful to them. Please add reply as soon as possible..
Thank You in advance, : (
Mike Young says
I want to add tag only on page of wordpress v4.5.2. By using the following code, gets tag in admin setting, but the tag doesn’t show up on page. How come?
// add tag support to pages
function tags_support_all() {
register_taxonomy_for_object_type(‘post_tag’, ‘page’);
}
// ensure all tags are included in queries
function tags_support_query($wp_query) {
if ($wp_query->get(‘tag’)) $wp_query->set(‘post_type’, ‘any’);
}
// tag hooks
add_action(‘init’, ‘tags_support_all’);
add_action(‘pre_get_posts’, ‘tags_support_query’);
Please help. Thanks in advance!
Mike Sanger says
Using on a new project, works great, thank you ShibaShake for the post and John for “the code”!
Dinesh says
Hi shiba,
This is exactly what I have been looking for. But I don’t seem to be able to get it to work. I’ve added the code to the functions.php in my child theme. I’ve got a template called “post.php” which is based on:
http://voodoopress.com/how-to-post-from-your-front-end-with-no-plugin/
I can’t seem to get the meta boxes to show. I’ve added the following div’s to post.php:-
TAG
Category
I’ve tried changing “init” to “admin_init”… but still no luck. Page just would show the meta boxes. (I’m using WordPress 3.5) What am I doing wrong?
Thank you and regards,
Dinesh, S.
ShibaShake says
I would try doing it on a standard setup first with a standard theme.