Categorizing Pages


Hello all,

Does anyone have a solution catagorizing pages much the same way you would for posts?


Hey Mike,

WordPress has API functions to register taxonomies for any post types. If you want to re-use and share the Posts taxonomy (called category) on pages, you may just register it as such:

add_action( 'init', 'example_register_category' );
/**
 * Register categories on pages.
 */
function example_register_category() {

  register_taxonomy_for_object_type( 'category', 'page' );

}

It is important to note that this will shares all category terms which might not be what you are after. If you need a separate taxonomy specific to pages, then you may register as such:

add_action( 'init', 'example_register_taxonomy' );
/**
 * Register taxonomy.
 */
function example_register_taxonomy() {
 $args = array(
    'labels'            => array(
     'name'              => _x( 'Examples', 'taxonomy general name', 'example-domain' ),
     'singular_name'     => _x( 'Example', 'taxonomy singular name', 'example-domain' ),
     'search_items'      => __( 'Search Examples', 'example-domain' ),
     'all_items'         => __( 'All Examples', 'example-domain' ),
      'parent_item'       => __( 'Parent Example', 'example-domain' ),
      'parent_item_colon' => __( 'Parent Example:', 'example-domain' ),
     'edit_item'         => __( 'Edit Example', 'example-domain' ),
      'update_item'       => __( 'Update Example', 'example-domain' ),
      'add_new_item'      => __( 'Add New Example', 'example-domain' ),
     'new_item_name'     => __( 'New Example Name', 'example-domain' ),
      'menu_name'         => __( 'Example', 'example-domain' ),
   ),
    'hierarchical'      => true,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'example' ),
  );

  // Register the taxonomy.
 register_taxonomy( 'example', array( 'page' ), $args );

 // Assign the taxonomy to pages.
  register_taxonomy_for_object_type( 'example', 'page' );
}

Not that the snippet above will add an "Example" taxonomy to the pages.

Happy coding,


Thanks Thierry,

Both cases will work great for me, however when navagating to the catagory I get a 404 page and the pages assigned to that catagory do not show up. In a case where a post and a page are assigned to a catagory, only the post will show up.

Cheers,

Mike


Hi Thierry,

If i change my permalinks to plain, the example_register_taxonomy() example works but the example_register_category() will still show a 404 page.

Cheers,


Hey Mike,

If you want to display your pages with your posts in the front end for shared categories, you will have to slightly modify the main query to accomodate pages (aka page post type). Here is how you can do so:

add_action( 'pre_get_posts', 'example_pre_get_posts' );
/**
 * Add pages to the query if it isn't the admin, the main query and a category.
 *
 * @param WP_Query $query The WP_Query instance (passed by reference).
 */
function example_pre_get_posts( $query ) {
  if ( true !== is_admin() && $query->is_main_query() && $query->is_category() ) {
    $query->set( 'post_type', array( 'post', 'page' ) );
  }
}

Happy coding,


Fantastic, ill give this a go. Thanks Thierry!


Hey Thiery!

I've been struggling with custom taxonomies and post types. I've registered custom post type as follows:

function products_post_type() {

  $labels = array(
    'name'                  => _x( 'Products', 'Post Type General Name', 'text_domain' ),
   'singular_name'         => _x( 'Product', 'Post Type Singular Name', 'text_domain' ),
   'menu_name'             => __( 'Products', 'text_domain' ),
   'name_admin_bar'        => __( 'Product', 'text_domain' ),
    'archives'              => __( 'Item Archives', 'text_domain' ),
    'attributes'            => __( 'Item Attributes', 'text_domain' ),
    'parent_item_colon'     => __( 'Parent Product:', 'text_domain' ),
    'all_items'             => __( 'All Products', 'text_domain' ),
   'add_new_item'          => __( 'Add New Product', 'text_domain' ),
    'add_new'               => __( 'New Product', 'text_domain' ),
    'new_item'              => __( 'New Item', 'text_domain' ),
   'edit_item'             => __( 'Edit Product', 'text_domain' ),
   'update_item'           => __( 'Update Product', 'text_domain' ),
   'view_item'             => __( 'View Product', 'text_domain' ),
   'view_items'            => __( 'View Items', 'text_domain' ),
   'search_items'          => __( 'Search products', 'text_domain' ),
    'not_found'             => __( 'No products found', 'text_domain' ),
    'not_found_in_trash'    => __( 'No products found in Trash', 'text_domain' ),
   'featured_image'        => __( 'Featured Image', 'text_domain' ),
   'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
   'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
    'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
    'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
   'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
    'items_list'            => __( 'Items list', 'text_domain' ),
   'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
    'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
  );
  $args = array(
    'label'                 => __( 'Product', 'text_domain' ),
    'description'           => __( 'Product information pages.', 'text_domain' ),
   'labels'                => $labels,
   'supports'              => array( 'title', 'thumbnail', 'page-attributes', ),
   'hierarchical'          => false,
   'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
   'menu_icon'             => 'dashicons-media-spreadsheet',
   'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,    
    'exclude_from_search'   => false,
   'publicly_queryable'    => true,
    'capability_type'       => 'page',
  );

  register_post_type( 'product', $args );

}

and the cutom taxonomy as follows:

function custom_product_taxonomy() {
  $labels = array(
    'name'          => 'Product Types',
   'singular_name'     => 'Product Type',
    'search_items'      => 'Search Product Types',
    'all_items'       => 'All Product Types',
   'parent_item'       => 'Parent Product Type',
   'parent_item_colon'   => 'Parent Product Type:',
    'edit_item'       => 'Edit Product Type',
   'update_item'     => 'Update Product Type',
   'add_new_item'      => 'Add New Product type',
    'new_item_name'     => 'New Product Type Name',
   'menu_name'       => 'Product Type'
 );

  $args = array (
   'hierarchical'      => true,
    'labels'        => $labels,
   'show_ui'       => true,
    'show_admin_column'   => true,
    'query_var'       => true,
    'public'        => true,
    'publicly_queryable'  => true, 
   'rewrite'       => array( 'slug' => 'product-type' )
  );

  register_taxonomy('product-type', array('product'), $args);
}

and that all works great however when creating the custom taxonomies and assigning the custom post type to it they don't show up (404 page) when the taxonomy is queried in the front end (ie from a menu link).

Not too sure where I'm going wrong or if i've missed something? Thanks TT

Cheers,


I solved the above by changing slug to 'product_type' and permalinks to numeric. 🙂


Fantastic, thanks for letting us know 🙂


I believe after follow up research I just needed to flush my permalinks after adding a new custom taxonomy. 🙂

Write a reply

Login or register to write a reply, it's free!