Add Tags and Categories to Your WordPress Page

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.

Related Articles

Comments

  1. beni says

    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

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

  3. 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 :)

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

  5. Aashutosh Kumar says

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

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

  7. John says

    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
    
  8. maria says

    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

  9. says

    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?

    • says

      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.

  10. says

    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');
    
  11. Vivek says

    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

    • says

      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.

  12. says

    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

  13. vincent says

    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 ?

  14. Harpinder Singh says

    Hi,

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

    Thanks,
    Harpinder

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

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

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

      • says

        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

          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.

  17. 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:

    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);
    		
    	}
    
  18. 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 :)

Trackbacks

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>