Display categories from custom post type

I have created custom post type ‘Portfolio’ also under this post type I have created Portfolio categories with following code

functions.php – following is the code for custom post type i have defined in functions.php page

Read More
 function demo_register_post_type() {
            register_post_type('Portfolio', array(
                'labels' => array(
                    'name' => __('Portfolio'),
                    'singular_name' => __('Portfolio'),
                    'add_new' => 'Add New Portfolio',
                    'edit_item' => 'Edit Portfolio',
                    'new_item' => 'New Portfolio',
                    'view_item' => 'View Portfolio',
                    'search_items' => 'Search Portfolio',
                    'not_found' => 'No Portfolio found',
                    'not_found_in_trash' => 'No Portfolio found in Trash',
                    'show_ui' => true,
                    'show_in_menu' => true,
                    'capability_type' => 'post',
                    'hierarchical' => false,
                    'rewrite' => true,
                    'query_var' => true
                ),
                'menu_position' => 6,
                'public' => true,
                'supports' => array(
                    'title','editor','author','thumbnail','excerpt','comments','page-attributes'
                ),
                'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false ),
                'taxonomy' => array('category', 'post_tag')
            ));

        register_taxonomy( 'portfolio-category', 'Portfolio', array ('hierarchical' => true, 'label' => __('Portfolio Categories')));  // portfolio categories

Now I want to retrieve all the categories from this post type and display posts from the categories on page?

Please let me know how could I do this?

Thanks.

Related posts

Leave a Reply

3 comments

  1. to get the posts of your custom post type you need to query post_type and you can do it like so:

    <?php query_posts(array( 'post_type' => 'Portfolio' )); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
     <div class="post">
    
     <!-- Display the Title as a link to the Post's permalink. -->
     <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
     <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
     <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
    
      <div class="entry">
        <?php the_content(); ?>
      </div>
    
      <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
     </div> <!-- closes the first div box -->
    
     <?php endwhile; else: ?>
     <p>Sorry, no posts matched your criteria.</p>
     <?php endif; ?>
    

    Now if you want to get Portfolio post of a specific term in your custom taxonamy
    then add the taxonomy argument to the query_posts array like so:

    <?php query_posts(array( 'post_type' => 'Portfolio','portfolio-category' => 'category-name' )); ?>
    

    Hope this helps.

  2. Try this code, it’s work. Thank me later.

       $term_args = array(
                    'post_type'              => 'portfolio',
                     'taxonomy'               => 'project-type',
                    'hide_empty'             => true,
                    'fields'                 => 'all'
                  );
       $term_query = new WP_Term_Query( $term_args );