0

I'm having a hard time explaining this, so bare with me.

Category and tag links for my custom post type aren't working as I expected them to. Instead of getting http://www.example.com/projects/category/example-category, the links that are http://www.example.com/category/example-category. In other words, I can't figure out how to make my categories post-type specific. In the dashboard, my Projects post type has both a Tags and a Categories section, but editing them results in the same changes being made to the main Posts post type.

How can I create post-type specific categories?

Here's my code to create the post type:

function projects_create_post_type() {
    $labels = array(
        "add_new" => "Add New",
        "add_new_item" => "Add New",
        "all_items" => "All Projets",
        "edit_item" => "Edit",
        "menu_name" => "Projects",
        "name" => "Projects",
        "new_item" => "New",
        "not_found" =>  "No projects found",
        "not_found_in_trash" => "No projects found in Trash",
        "search_items" => "Search Projects",
        "singular_name" => "Project",
        "view_item" => "View",
    );
    $args = array(
        "has_archive" => true,
        "hierarchical" => false,
        "labels" => $labels,
        "menu_icon" => __("dashicons-hammer"),
        "menu_position" => 5,
        "public" => true,
        "slug" => "projects",
        "supports" => array(
            "editor",
            "thumbnail",
            "title",
        ),
        "taxonomies" => array("post_tag", "category"),
    );
    register_post_type("projects", $args);
}
add_action("init", "projects_create_post_type");

2 Answers 2

3

You need to create taxonomies specific to your post type using register_taxonomy instead of using the taxonomies argument when registering the post type.

http://codex.wordpress.org/Function_Reference/register_taxonomy

// categories
register_taxonomy(
  'project_categories',
  'projects',
  array(
    'label'        => __( 'Categories' ),
    'rewrite'      => false,
    'hierarchical' => true,
    'capabilities' => array( 'edit_terms' => 'manage_categories' )
  )
);

// tags
register_taxonomy(
  'project_tags',
  'projects',
  array(
    'label'        => __( 'Tags' ),
    'rewrite'      => false,
    'hierarchical' => false,
    'capabilities' => array( 'edit_terms' => 'manage_categories' )
  )
);
0
-1

If you need to output tags for custom post type - here good example https://jamper.online/vyvod-tegov-tags-dlya-kastomnyh-postov-custom-post-type

    function get_terms_by_custom_post_type( $post_type, $taxonomy ){
    $args = array( 'post_type' => $post_type);
    $loop = new WP_Query( $args );
    $postids = array();
    // build an array of post IDs
    while ( $loop->have_posts() ) : $loop->the_post();
        array_push($postids, get_the_ID());
    endwhile;
    // get taxonomy values based on array of IDs
    $taxonomies = wp_get_object_terms( $postids,  $taxonomy );
    wp_reset_postdata();
    return $taxonomies;
}
2
  • the OP isn't asking how to display tags/categories. Also don't post the same answer multiple times, if you find duplicate questions flag them as duplicates so moderation can resolve it
    – Tom J Nowell
    Commented Sep 15, 2023 at 12:06
  • ok, sorry. thank you
    – Platon
    Commented Sep 16, 2023 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.