• I have a CPT of ‘item’, for which all posts will be in a single category ‘magazine’.

    I don’t know whether it’s better to create a normal category and then auto-assign item posts to it (I know how to do that)… or create a custom taxonomy so that item posts are assigned to it (I don’t know how to do this, and in trying to explain here am already beyond my knowledge).

    The ideal is that compose/edit page for normal posts doesn’t show a magazine category, and the compose/edit page for the item cpt doesn’t show any categories but all item posts are auto-assigned to that category.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    It seems to me that if use a CPT and would use it with only one category, you don’t need the category at all. site.com/cpt-slug/ will return the same thing as the category.

    Thread Starter gulliver

    (@gulliver)

    Thanks.

    Yes, you’re right, and there’s other info which I should have mentioned…

    I currently have the slug set as ‘magazine’, so the url path is /magazine/item-name/ and hence works as you suggest.

    The reason for assigning a category is for use with a third-party membership script, for which assigning a category (magazine) to be protected is a better option than setting protection on individual pages.

    By having a category of ‘magazine’, to which I can assign (currently done automatically) all ‘item’ posts, I’m thus still able to show an unprotected ‘teaser’ page (ordinary, not cpt, page) at an url of /magazine/.

    Having initially struggled to comprehend CPTs, I’ve subsequently been lazy and not revisited to understand the whole taxonomy thing – hence my post… I don’t know if registering a taxonomy of ‘magazine’ will add such a check-box to the category metabox and whether it’ll be available for just CPTs or also ordinary posts.

    I really should be more self-responsible and dive-in rather than hesitating.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I’d use a custom taxonomy to keep things separate from posts. If you add a custom taxonomy and set it to be part of the CPT in the CPT’s definition, you’ll get a metabox on the CPT add/update page.

    Thread Starter gulliver

    (@gulliver)

    Thanks.

    Having experimented, I think I understand this better – but there’s still some issue which puzzle me.

    I can stop the normal/post categories metabox appearing on the edit screen for the ‘item’ cpt by not using:

    'taxonomies' => array( 'post_tag', 'category' ).

    And I’ve added a taxonomy which adds a menu item and metabox for the ‘item’ cpt, using:

    function build_taxonomies() {
    register_taxonomy( 'categories', 'item', array( 'hierarchical' => true, 'label' => 'Special Categories', 'query_var' => true, 'rewrite' => true ) );
    }

    But if I then create a new category using that ‘item’ metabox, the category also appears in the metabox of normal posts. I guess I’m missing something which would stop this.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    change the line to

    ‘taxonomies’ => array( ‘your_new_taxonomy’ )`

    Thread Starter gulliver

    (@gulliver)

    Thanks.

    This initially caused a duplicate category issue, which I began to describe before realising the cause was that the code I use to auto-assign ‘item’ CPTs to ‘magazine’ category is probably creating the duplicates.

    Removing it seems to sort the issue. So now I need to find a way to auto-assign without creating duplicate categories.

    function assign_item_category_automatically($post_ID) {
    global $wpdb;
    if(!wp_is_post_revision($post_ID)) {
    wp_set_object_terms( $post_ID, 'magazine', 'category');
    }
    }
    add_action('publish_item', 'assign_item_category_automatically');

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Use category, or custom taxonomy, for CPT?’ is closed to new replies.