• Hi

    I’ve added a custom post type to my site called ‘suppliers’ and set up categories within that custom post type by adding the following to funtions.php.

    function my_custom_post_supplier() {
      $labels = array(
        'name'               => _x( 'Suppliers', 'post type general name' ),
        'singular_name'      => _x( 'Supplier', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'book' ),
        'add_new_item'       => __( 'Add new supplier' ),
        'edit_item'          => __( 'Edit suppliers' ),
        'new_item'           => __( 'New supplier' ),
        'all_items'          => __( 'All suppliers' ),
        'view_item'          => __( 'View suppliers' ),
        'search_items'       => __( 'Search suppliers' ),
        'not_found'          => __( 'No suppliers found' ),
        'not_found_in_trash' => __( 'No suppliers found in the Trash' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'Suppliers'
      );
      $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our products and product specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
        'has_archive'   => true,
      );
      register_post_type( 'supplier', $args );
    }
    add_action( 'init', 'my_custom_post_supplier' );

    I’ve then got it to list all the categories within supplier by adding the following code to a page template.

    <?php
    $customPostTaxonomies = get_object_taxonomies('supplier');
    
    if(count($customPostTaxonomies) > 0)
    {
         foreach($customPostTaxonomies as $supplier)
         {
    	     $args = array(
             	  'orderby' => 'name',
    	          'show_count' => 0,
            	  'pad_counts' => 0,
    	          'hierarchical' => 1,
            	  'taxonomy' => $supplier,
            	  'title_li' => ''
            	);
    
    	     wp_list_categories( $args );
         }
    } ?>

    When I click one of these links it goes to
    http://www.mysite.co.uk/supplier_category/categoryname

    With categoryname being the name of the link I just clicked.

    When do this using normal posts (not custom) this then works by going to my archive.php page to detect what category was clicked and to display the posts within that category. Using custom posts I just get page not found.

    I’m using a bespoke theme which used _me (underscoresme) as a starting point.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The best way to add Categories to a custom post type is to add this to your $args for the register_post_type() function :
    'taxonomies' => array( 'category' );
    http://codex.wordpress.org/Function_Reference/register_post_type#Taxonomies

    I’m not quite clear for your description, what’s happening on your site. It’d help if your posted your URL so we could see exactly what’s URLs are being generated.

    As you stated, a category URL tells WordPress to use archive.php (if there’s no category.php):
    http://codex.wordpress.org/Template_Hierarchy#Category_display

    The category URL itself is formed using the Category base (default: “category”) set in your admin Permalinks panel. So the default should be (whether for native posts or custom post types):
    example.com/category/{category-slug}

    Have you set “supplier_category” as you Category base?

    Moderator bcworkz

    (@bcworkz)

    There’s some quirks with the default category taxonomy and custom post types, but what you describe is beyond that. Clicking a category link from a custom post type should yield regular posts in that category, though your CPT will not be in the list. Could it be the category term you clicked from your CPT is not assigned to any regular posts at all? That would explain the posts not found.

    To get CPTs to be listed in category queries, you need to hook ‘pre_get_posts’. If you get the ‘post_type’ query var there, it’ll probably be array('post'); Add your CPT to this array and set the modified array back to ‘post_type’. Of course, be sure your script verifies it’s a category query being handled before doing anything.

    For some reason we don’t need to do this with custom taxonomy queries, only the default category and post_tag taxonomies have this quirk.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom post categories’ is closed to new replies.