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 to Dinesh Cancel reply

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

Comments

« Previous 1 2 3 4 Next »
  1. 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
  2. rasel says

    November 22, 2014 at 5:00 am

    How to show this categories on front end.

    Reply
  3. AFril says

    September 22, 2014 at 8:49 am

    Thanks! This saves my life ^_^

    Reply
  4. 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
  5. 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
  6. 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
  7. Markee says

    May 16, 2013 at 5:35 am

    thx, works like a charm for me!

    Reply
  8. 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
  9. 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
  10. 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
« Previous 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 ·