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.
Harpinder Singh says
Hi,
Could someone tell me how do i retrieve all tags that are posted while adding pages?
Thanks,
Harpinder
ShibaShake says
wp_get_object_terms
http://codex.wordpress.org/Function_Reference/wp_get_object_terms
Tim Marshall says
Hi
Any chance you could elaborate on how to display on a page the Category ID’s that have been selected.
I’ve looked at the codex and I just don’t get it 🙁
I can’t get wp_get_object_terms to return me anything, the other stuff sets and saves brilliantly though….
Half way there 🙁
Sorin Haidau says
Here’s an example on retrieving the tags attached to a page (in my case $work): — the good example —
Harpinder Singh says
Great Info! Really helpful to me. thanks for posting…
Sandra says
Where is the admin_init hook located? I cant find it in the functions php
ShibaShake says
What theme are you using?
Jake says
I’m also having trouble finding the admin_init hook. Can you have a look at my function.php file I have shared as google doc under my website field in this form and let me know where to put it. Thanks
ShibaShake says
That admin_init hook could also be in any of the included files. Or if there isn’t an admin_init hook, then you can add one using the add_action function.
Jaap says
Great article – very helpful. I can now get my pages to display its tags, but not its categories – is there any reason category template tags (ie the_category() wouldn’t work with this solution?
Thanks!
Lionel says
If we do these changes will auto tagging plugins like Strictly Auto Tags tag the content of pages?
ShibaShake says
That will depend on the plugin and whether it checks for taxonomy relationships with various custom post types. It is best to ask the plugin developer.
LL says
Shiba, thanks for sharing this– I’ve been looking for a plug-in that does this but couldn’t get one. I am still having a slight issue, hoping someone could help: I added the two lines register_taxonomy of code in the initialization function. This made the Category and Tag boxes show on my page edit screen, however the categories don’t seem to paste. What am I doing wrong?
Thanks in advance!
ShibaShake says
When you say the categories don’t seem to paste, do you mean they do not get saved? Do the tags get saved?
The Shepherd says
I’m pretty sure I’ve tried this before and it would work, but now my categories don’t seem to save.
The tags save just fine.
Any ideas?
GKY says
It saves categories and tags for pages.But when you want to display it in loop you have to add ‘post_type’ => ‘page’ with quotes under query_post array.If you don’t do that wordpress thinks user wants posts in a category not pages.
Trista Yard says
I used ‘taxonomies’ => array(‘category’), to get the categories meta box on my custom post page, for some reason the categories do not seem to be updating (when I erase a category it is still there as an option on my custom post page). Do you know why? This is the code I used:
ShibaShake says
Hmmm, it looks right to me. The bug may be coming from somewhere else.
Michael W says
Stumped. Tried adding your code to functions.php (within the twentyten theme), using WP 3.0.1. Nothing changed in my page in the admin UI. Suggestions?
ShibaShake says
Hmmm, strange. Try adding it in the admin_init action hook.
Julia K says
I had the same problem as Michael and adding it in the admin_init action hook worked. Thanks!
XYDAC says
Hey i think after using register_taxonomy_for_object_type(), WordPress automatically creates respective Meta-box,so why do you need to add them.
We can use only register_taxonomy_for_object_type(),and that should be all, adding the request filter was important as WordPress doesn’t automatically links pages to categories in WP 3.0
Nice Work 🙂
ShibaShake says
Thanks for letting me know. I have included it in the article. Great catch!