Shiba

Adventures in WordPress

  • — Home —
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Programming / Add Tags and Categories to Your WordPress Page

Add Tags and Categories to Your WordPress Page

Tweet

by ShibaShake 62 Comments

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');
Added Tag and Category Metabox to our WordPress Page.
Added Tag and Category Metabox to our WordPress Page.

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');                
Categories are now properly attached to your WordPress page.
Categories are now properly attached to your WordPress 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.

Related Articles

Add a Metabox to Your Custom Post Type Screen

How to add our own metabox to the edit custom post type screen.

Add Custom Taxonomy Tags to Your WordPress Permalinks

How to include taxonomy tags into our custom post type permalinks.

Optimize Your WordPress Plugins with Tags

Shows how you can enhance page speed by using tags to selectively run plugins only when needed. Page speed is important because it affects Google Page Rank and user experience.

Comments

  1. Vlad says

    September 4, 2015 at 12:16 am

    Old post. As of 3.0

    add_action('init', 'add_category_metabox');
    function add_category_metabox() {
    register_taxonomy_for_object_type('category', 'page');
    }

    is enough

    Reply
  2. m says

    February 20, 2015 at 10:49 am

    This is probably an obvious one, but can this code be used on a wp.com theme and editing under the customize–>css segment? Trying yet another shot in the dark here and any help would be appreciated.

    Reply
    • ShibaShake says

      February 20, 2015 at 4:49 pm

      No, this requires PHP code.

      Reply
  3. Rawan says

    January 12, 2015 at 3:31 am

    Hey I have created a category in pages section but how to display the pages who add on the particular category

    Reply
  4. beni says

    December 19, 2014 at 2:46 am

    i just cant add category and tag on firefox and chrome browser, if i try add tag and category with opera browser its work. i have reinstall and install firefox and remove all addon firefox but its still not working. I do deactive and active plugin wordpress that is the same, i cant add catergory and tag.
    oh ya, this morning i have update wordpress with the last version 4.1

    Reply
  5. Sougata says

    December 5, 2014 at 11:27 am

    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

    Reply
  6. rasel says

    November 22, 2014 at 5:00 am

    How to show this categories on front end.

    Reply
  7. AFril says

    September 22, 2014 at 8:49 am

    Thanks! This saves my life ^_^

    Reply
  8. Pramesh Pudasaini says

    June 29, 2014 at 6:23 am

    I want to remove tags and categories from the home page. How do I do it?

    Reply
  9. Joe says

    April 4, 2014 at 3:29 am

    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 ๐Ÿ™‚

    Reply
  10. ashleigh says

    July 7, 2013 at 6:37 pm

    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.

    Reply
    • ShibaShake says

      July 11, 2013 at 2:09 pm

      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

      Reply
  11. Markee says

    May 16, 2013 at 5:35 am

    thx, works like a charm for me!

    Reply
  12. Aashutosh Kumar says

    May 8, 2013 at 3:21 am

    Fine, But My problem is i have added

    function add_category_box_on_page(){
        //add meta box
    	
    	// Add category metabox to page 
        add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'side', 'low');
    	//register_taxonomy_for_object_type('category', 'page');
    	
    	// Add tag metabox to page
        add_meta_box('tagsdiv-post_tag', __('Tags'), 'post_tags_meta_box', 'page', 'side', 'low');
    	//register_taxonomy_for_object_type('post_tag', 'page');
    	
        add_action('save_post', 'my_func_cats_tags_save_postdata');
    }
     
    add_action('admin_menu', 'add_category_box_on_page');
     
    function my_func_cats_tags_save_postdata( $post_id ) {
     
        $wpnj_post_type = $_POST['post_type'];
     
    	    // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
    	    // to do anything
    	    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
     
        }else{
            // Check permissions
            if ( 'page' == $wpnj_post_type ) {
                if ( current_user_can( 'edit_page', $post_id ) ){
     
                    $wpnj_post_cats = array();
     
                    foreach($_REQUEST['post_category'] as $key=>$val){
                        $wpnj_post_cats[] = $val;
                    }
                    wp_set_post_categories( $post_id, $wpnj_post_cats );
                }
            }
        }
    }
    

    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, : (

    Reply
    • Mike Young says

      June 12, 2016 at 10:00 am

      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!

      Reply
  13. Mike Sanger says

    February 6, 2013 at 12:55 am

    Using on a new project, works great, thank you ShibaShake for the post and John for “the code”!

    Reply
  14. Dinesh says

    January 29, 2013 at 5:19 am

    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.

    Reply
    • ShibaShake says

      January 31, 2013 at 8:02 am

      I would try doing it on a standard setup first with a standard theme.

      Reply
  15. Johan van de Merwe says

    January 4, 2013 at 2:18 pm

    Works like a charm! Thank you.

    Reply
    • rasel says

      November 22, 2014 at 5:04 am

      how do you retrieve attached categories of front page?

      Reply
  16. John says

    October 12, 2012 at 9:32 pm

    The Code

    //Pages Tags & Category Meta boxes
    function add_pages_meta_boxes() {
    add_meta_box(	'tagsdiv-post_tag', __('Page Tags'), 'post_tags_meta_box', 'page', 'side', 'low');
    add_meta_box(	'categorydiv', __('Categories'), 'post_categories_meta_box', 'page', 'normal', 'core');
    }
    add_action('add_meta_boxes', 'add_pages_meta_boxes');
    
    add_action('init','attach_category_to_page');
    function attach_category_to_page() {
        register_taxonomy_for_object_type('category','page');
    }
    //end
    
    Reply
    • Simone says

      April 15, 2013 at 8:18 am

      Hi,
      I add your suggested code in my source code to add Category on my pages.
      Now, I’d like to create a sidebar that show pages of a similar Category.

      Can you help me, please?

      Thank you.
      Simone

      Reply
    • Maya says

      August 2, 2013 at 3:38 am

      Great! Thanks!

      Reply
    • Kar.ma says

      June 3, 2014 at 12:53 pm

      Thanks so much, this fixed my error (Call to undefined function add_meta_box()).

      Reply
  17. John says

    October 12, 2012 at 9:31 pm

    Here’s the completed code that works and saves the cats and tags.

    Reply
  18. colin says

    September 17, 2012 at 2:44 pm

    how do i show pages from one category? query posts doesn’t seem to go looking for pages. ??

    Reply
    • ShibaShake says

      September 18, 2012 at 9:53 pm

      One possibility is to hook into the ‘request’ filter.
      http://shibashake.com/wordpress-theme/mastering-the-wordpress-loop

      Reply
  19. maria says

    September 10, 2012 at 2:13 am

    I think I have the same problem than Jaap: It works fine, I can add tags and categories to the pages, but when I call the categories whithin the loop (with the_category()) to show them on the page they don’t appear, but tags do. Somebody knows how can I show them? Thanks

    Reply
  20. Geoffrey Hale says

    August 3, 2012 at 2:56 pm

    This is awesome! Thank you. Will our permalink structure settings apply to pages? Like if we set /%category%/%post%/, will that also result in /%category%/%page%/? Can we do this with Yoast’s SEO Plugin?

    Reply
    • ShibaShake says

      August 3, 2012 at 5:02 pm

      Will our permalink structure settings apply to pages?

      No. You will have to manually set this if you want to change page permalinks.

      Can we do this with Yoastโ€™s SEO Plugin?

      Don’t know. I have never used this plugin.

      Reply
  21. QLStudio says

    May 19, 2012 at 8:33 am

    This seems to be play happy with latest WP:

    	// Add to the admin_init hook of your theme functions.php file
    	add_action( 'add_meta_boxes', 'qlstudio_add_page_tags' );
    	function qlstudio_add_page_tags() {
    		add_meta_box( 'tagsdiv-post_tag', __('Page Tags'), 'post_tags_meta_box', 'page', 'side', 'low' );
    	}
    	register_taxonomy_for_object_type('post_tag', 'page');
    
    Reply
    • Hugo says

      May 21, 2012 at 6:41 pm

      And how i display the tags on my theme?

      Reply
  22. Vivek says

    December 16, 2011 at 4:30 am

    Hi,

    Thanks for this code, i was looking for this kind of plugin where i can add categories with my pages. Although it is not exactly what i need but a bit helpful. I have a question :

    Is there any way using your code or some extra bit of code, we can retrieve only specific category and it’s subcategories and show on pages the same way you are showing on “add page” section ?

    Please help.

    Thanks
    Vivek

    Reply
    • ShibaShake says

      January 4, 2012 at 3:15 pm

      Yes. I would probably just copy the existing meta-box code for categories, and then alter that to only show the categories you want. Then use add_meta_box to add it to the page.

      Reply
  23. Brandon Bowman says

    November 15, 2011 at 11:56 am

    I am trying to implement this. I am using WordPress 3.2.1. When I add the code above to the functions.php, it returns an error Call to undefined function add_meta_box(). I am using the TwentyTen themes function.php. Is this just because I am putting it in the wrong place or what?

    Thanks

    Reply
    • ShibaShake says

      November 22, 2011 at 11:25 am

      It could be that you included the function call before the proper file was loaded. Check out the WordPress Codex for a detailed example of where to add the add_meta_box function call.
      http://codex.wordpress.org/Function_Reference/add_meta_box

      Reply
  24. vincent says

    November 7, 2011 at 9:34 am

    Hi !

    I would like , how to add Category meta box and my admin page ?
    I have developped my own plugin and i need to use WP category and i want to display div id= “categorydiv” in my admin_plugin page?
    Its possible ?

    Reply
    • ShibaShake says

      November 8, 2011 at 11:30 am

      http://shibashake.com/wordpress-theme/standard-wordpress-metabox

      Reply
  25. Harpinder Singh says

    June 20, 2011 at 4:31 am

    Hi,

    Could someone tell me how do i retrieve all tags that are posted while adding pages?

    Thanks,
    Harpinder

    Reply
    • ShibaShake says

      June 23, 2011 at 10:02 am

      wp_get_object_terms
      http://codex.wordpress.org/Function_Reference/wp_get_object_terms

      Reply
      • Tim Marshall says

        July 4, 2011 at 1:52 pm

        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 ๐Ÿ™

        Reply
        • Sorin Haidau says

          February 19, 2013 at 12:46 pm

          Hereโ€™s an example on retrieving the tags attached to a page (in my case $work): — the good example —

          foreach($works as $work){
                $tags = wp_get_object_terms($work->ID, 'post_tag');
                if(!empty($tags)){
                      if(!is_wp_error( $tags )){
                            foreach($tags as $tag){
                                  echo get_term_link($tag->slug, 'post_tag');  // the link
                                  echo $tag->name;  // the name
                            }
                      }
                }
          }
          
          Reply
  26. Harpinder Singh says

    June 20, 2011 at 4:23 am

    Great Info! Really helpful to me. thanks for posting…

    Reply
  27. Sandra says

    June 14, 2011 at 4:53 pm

    Where is the admin_init hook located? I cant find it in the functions php

    Reply
    • ShibaShake says

      June 16, 2011 at 1:13 pm

      What theme are you using?

      Reply
      • Jake says

        July 25, 2011 at 11:18 pm

        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

        Reply
        • ShibaShake says

          July 28, 2011 at 1:14 pm

          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.

          Reply
  28. Jaap says

    April 25, 2011 at 2:58 pm

    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!

    Reply
  29. Lionel says

    March 29, 2011 at 1:49 pm

    If we do these changes will auto tagging plugins like Strictly Auto Tags tag the content of pages?

    Reply
    • ShibaShake says

      March 30, 2011 at 2:22 pm

      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.

      Reply
  30. LL says

    January 5, 2011 at 3:28 am

    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!

    Reply
    • ShibaShake says

      January 9, 2011 at 10:00 pm

      When you say the categories don’t seem to paste, do you mean they do not get saved? Do the tags get saved?

      Reply
      • The Shepherd says

        March 19, 2012 at 4:13 am

        I’m pretty sure I’ve tried this before and it would work, but now my categories don’t seem to save.

        add_meta_box( 'tagsdiv-post_tag',	__('Page Tags'),	'post_tags_meta_box',		'page',	'side',	'low');
        register_taxonomy_for_object_type('post_tag', 'page');
        
        add_meta_box( 'categorydiv',		__('Categories'),	'post_categories_meta_box',	'page',	'side',	'core');
        register_taxonomy_for_object_type('category', 'page');
        

        The tags save just fine.
        Any ideas?

        Reply
        • GKY says

          April 1, 2013 at 3:24 am

          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.

          Reply
  31. Trista Yard says

    December 8, 2010 at 3:51 pm

    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:

    add_action('init', 'product_register');
    
    	function product_register() {
        	$args = array(
            	'label' => __('Products'),
            	'singular_label' => __('Product'),
            	'public' => true,
            	'show_ui' => true,
            	'capability_type' => 'post',
            	'hierarchical' => false,
            	'rewrite' => false,
    			'taxonomies' => array( 'category'),
            	'supports' => array('title', 'editor', 'thumbnail')
            );
    
        	register_post_type('product' , $args);
    		
    	}
    
    Reply
    • ShibaShake says

      December 9, 2010 at 8:47 am

      Hmmm, it looks right to me. The bug may be coming from somewhere else.

      Reply
  32. Michael W says

    October 28, 2010 at 5:17 pm

    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?

    Reply
    • ShibaShake says

      November 2, 2010 at 1:58 pm

      Hmmm, strange. Try adding it in the admin_init action hook.

      Reply
      • Julia K says

        November 2, 2010 at 6:19 pm

        I had the same problem as Michael and adding it in the admin_init action hook worked. Thanks!

        Reply
  33. XYDAC says

    October 8, 2010 at 8:44 pm

    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 ๐Ÿ™‚

    Reply
    • ShibaShake says

      October 9, 2010 at 9:54 am

      Thanks for letting me know. I have included it in the article. Great catch!

      Reply

Leave a Reply to Sandra Cancel reply

Your email address will not be published.

Recent Posts

  • Screen-shot of mobile responsive Poll Daddy object, where text floats properly to the right of radio buttons.How to Make Poll Daddy Objects Mobile Responsive
  • Screen-shot of blog post with no page border (flowing design).Genesis Skins 1.5
  • Screen-shot of the media manager Create-Gallery screen, while doing a post search.Shiba Media Library 3.7
  • Screenshot of the Edit Gallery screen after we hit the Create Gallery button.How to Expand the WordPress Media Manager Interface
  • Blonde girl looking through and holding a circular picture frame.Shiba Gallery 4.3
  • Close-up of beautiful blonde holding a square picture frame.Google Authorship - Good or Bad for Search Traffic?
  • Shiba Widgets 2.0
  • Media Temple screenshot of editing my sub-domain DNS entry.Using CDN Cnames with w3tc and MultiSite
  • Shiba Skins WordPress ThemeShiba Skins WordPress Theme
  • How to add the Media Manager Menu to the Theme Preview InterfaceHow to Add the Media Manager Menu to the Theme Preview Interface

Recent Comments

  • WordPress Search Widget – How to Style It (56)
    • Nelson
      - Tanks master - Fine
    • TelFiRE
      - How do you style the "X" that shows up when you start typing?
  • Update custom inputs with the proper data using Javascript.Expand the WordPress Quick Edit Menu (58)
    • Mike G
      - This is exactly what is happening to me. It is updating the value in the database and in the column, but I have to refresh ...
    • PhoenixJP
      - Thanks for this tutorial. Does someone knows if this still work with wordpress 5.03 and 5.1.
    • Francine Carrel
      - This is a very long time away from your original comment, but did you ever work it out? I am stuck on the exact same thing ...
  • Custom meta-box with a set of radio-buttons.Add a Metabox to Your Custom Post Type Screen (27)
    • mike
      - Hi Shiba am a newbie to wordpress, I just installed a plugin with a custom post type, but has no option for discussion and ...
  • Write a Plugin for WordPress Multi-Site (45)
    • Andrew
      - Hi,action 'wpmu_new_blog' has been deprecated. Use โ€˜wp_insert_siteโ€™ instead.
  • Populate our Edit Gallery menu using a gallery shortcode.How to Add the WordPress 3.5 Media Manager Interface – Part 2 (29)
    • Janine
      - Still relevant in 2019.
  • WordPress Excerpt – How to Style It (36)
    • James
      - Great post. I really need some help. I have set border lines around my excerpts on my blog page/post page. The problem is ...
  • Add Custom Taxonomy Tags to Your WordPress Permalinks (123)
    • Darshan Saroya
      - Update permalinks. Go to settings > permalink and just save it.

Copyright © 2021 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·