0

I've created have a custom post type called "Crew", like so:

function crew_custom_post_type() {

  $labels = array(
    'name'                  => _x( 'Pages', 'Post Type General Name', 'project' ),
    'singular_name'         => _x( 'Page', 'Post Type Singular Name', 'project' ),
    'menu_name'             => __( 'Crew', 'project' ),
    'name_admin_bar'        => __( 'Crew', 'project' ),
    'archives'              => __( 'Item Archives', 'project' ),
    'attributes'            => __( 'Item Attributes', 'project' ),
    'parent_item_colon'     => __( 'Parent Item:', 'project' ),
    'all_items'             => __( 'All Items', 'project' ),
    'add_new_item'          => __( 'Add New Item', 'project' ),
    'add_new'               => __( 'Add New', 'project' ),
    'new_item'              => __( 'New Item', 'project' ),
    'edit_item'             => __( 'Edit Item', 'project' ),
    'update_item'           => __( 'Update Item', 'project' ),
    'view_item'             => __( 'View Item', 'project' ),
    'view_items'            => __( 'View Items', 'project' ),
    'search_items'          => __( 'Search Item', 'project' ),
    'not_found'             => __( 'Not found', 'project' ),
    'not_found_in_trash'    => __( 'Not found in Trash', 'project' ),
    'featured_image'        => __( 'Featured Image', 'project' ),
    'set_featured_image'    => __( 'Set featured image', 'project' ),
    'remove_featured_image' => __( 'Remove featured image', 'project' ),
    'use_featured_image'    => __( 'Use as featured image', 'project' ),
    'insert_into_item'      => __( 'Insert into item', 'project' ),
    'uploaded_to_this_item' => __( 'Uploaded to this item', 'project' ),
    'items_list'            => __( 'Items list', 'project' ),
    'items_list_navigation' => __( 'Items list navigation', 'project' ),
    'filter_items_list'     => __( 'Filter items list', 'project' ),
  );
  $args = array(
    'label'                 => __( 'Page', 'project' ),
    'description'           => __( 'Post Type for Crew', 'project' ),
    'labels'                => $labels,
    'supports'              => array( 'title','author' ),
    'taxonomies'            => array( 'crew' ),
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    '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( 'crew', $args );

}
add_action( 'init', 'crew_custom_post_type', 0 );

I've then created a taxonomy:

function crew_taxonomy() {

  $labels = array(
    'name'                       => _x( 'Categories', 'Taxonomy General Name', 'project' ),
    'singular_name'              => _x( 'Category', 'Taxonomy Singular Name', 'sabsound' ),
    'menu_name'                  => __( 'Categories', 'sabsound' ),
    'all_items'                  => __( 'All Items', 'sabsound' ),
    'parent_item'                => __( 'Parent Item', 'sabsound' ),
    'parent_item_colon'          => __( 'Parent Item:', 'sabsound' ),
    'new_item_name'              => __( 'New Item Name', 'sabsound' ),
    'add_new_item'               => __( 'Add New Item', 'sabsound' ),
    'edit_item'                  => __( 'Edit Item', 'sabsound' ),
    'update_item'                => __( 'Update Item', 'sabsound' ),
    'view_item'                  => __( 'View Item', 'sabsound' ),
    'separate_items_with_commas' => __( 'Separate items with commas', 'sabsound' ),
    'add_or_remove_items'        => __( 'Add or remove items', 'sabsound' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'sabsound' ),
    'popular_items'              => __( 'Popular Items', 'sabsound' ),
    'search_items'               => __( 'Search Items', 'sabsound' ),
    'not_found'                  => __( 'Not Found', 'sabsound' ),
    'no_terms'                   => __( 'No items', 'sabsound' ),
    'items_list'                 => __( 'Items list', 'sabsound' ),
    'items_list_navigation'      => __( 'Items list navigation', 'sabsound' ),
  );
  $args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
  );
  register_taxonomy( 'crew', array( 'crew' ), $args );

}
add_action( 'init', 'crew_taxonomy', 0 );

I've created a whole bunch of categories and assigned them to posts within the crew custom post type.

On the archive page, I would like to output the assigned categories for each post.

I've tried the following on archive-crew.php:

<?php

  // Begin loop
  while ( have_posts() ): the_post();

  // Get assigned categories for each post
  $post_id = get_the_ID();
  $array = array(
    'taxonomy' => 'crew',
  );

  $categories = wp_get_post_categories( $post_id, $array );

?>


  <?php
    echo '<h2>'.get_the_title().'</h2>';
    echo '<p>'.$categories.'</p>';
  ?>


<?php // End loop
  endwhile;
?>

The above doesn't return anything. I'm simply looking to list the child categories for filtering without any html around it.

  • wp_get_post_categories() is only for Core Categories - not custom taxonomies. Try wp_get_post_terms(). :) – WebElaine May 4 '18 at 13:21
  • Thank you! wp_get_object_terms did the trick. Please leave as an answer and I'll mark as solved. – Sam May 4 '18 at 14:12
1

wp_get_post_categories() is only for Core Categories - not custom taxonomies. Try wp_get_post_terms(). :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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