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

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comments

1 2 3 4 Next »
  1. 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 πŸ™

      • 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
                          }
                    }
              }
        }
        
  2. Harpinder Singh says

    June 20, 2011 at 4:23 am

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

    Reply
  3. 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

      • 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.

  4. 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
  5. 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
  6. 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?

      • 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.

  7. 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
  8. 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!

  9. 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
1 2 3 4 Next »

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS) (1)
    • Erik
      - Great article. All worked great except for this step:apt install php-mysqlChanging to this fixed it:apt install ...
  • Add Custom Taxonomy Tags to Your WordPress Permalinks (125)
    • Anthony
      - Where does this code go? Like, what exact .php file please?
  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text (1)
    • tom
      - hi,my experience was like the same, but for me as theme developer the "lazy blocks"(https://wordpress.org/plugins/lazy-blocks/) ...
  • WordPress Custom Taxonomy Input Panels (106)
    • Phil T
      - This is unnecessarily confusing. Why declare a variable with the same name as your taxonomy? Why did you choose a taxonomy ...
  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...

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