Fetch category from custom post type

I bought a WordPress Theme hoping that after some code customisation it should achieve what I want.

Now, I have (what I believe it is) a custom post type called ‘Portofolio’. As you can see in the picture below, it has the portofolio entries (at all portofolio) and categories for the aforementioned portofolio entries.

Read More

enter image description here

What I am trying to achieve is listing on a custom template page all the categories of the portofolio. So far I have this code but all it does is to fetch the entries of the portofolio not the categories.

    <?php
//$args = array('post_type' => 'tm_portfolio');
$term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'ids'] );
$args = [
    'tax_query' => [
        [
            'taxonomy' => 'tm_portfolio_category',
            'terms' => $term_ids
        ]
    ]
];
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  echo 'List of categories';
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

As you can see in the code, as the first line, I tried to fetch from the custom post type, but I had the same result.

I figured out the name/slug of the post type/taxonomy by checking the link in the admin panel while adding a category (check the picture below).
enter image description here

Related posts

1 comment

  1. I haven’t looked too much into the code, but I can see from the start that this line is not right.

    $term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'ids'] );
    

    it should be “id”

    e.g.

    $term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'id'] );
    

    EDIT

    Sorry my bad,

    You can try this approach instead

    $term_ids = get_terms( 'tm_portfolio_category', ['fields' => 'ids'] );
    
    $posts = query_posts( array(
        'post_type' => 'tm_portfolio',
        'tax_query' => array(
            array(
                'taxonomy' => 'tm_portfolio_category',
                'terms'    => $term_ids,
                )
            )
        ));
    
    foreach ($posts as $post) {
       echo 'List of categories';
       ?>
       <p><a href="<?php echo get_permalink($post->ID); ?>" title="Permanent Link to <?php echo the_title_attribute(array('post'=>$post->ID)); ?>">
       <?php echo get_the_title($post->ID); ?>
        </a></p>
       <?php
    }
    wp_reset_query();
    

    I would not recommend using the native WordPress Loop in this case for the sake of flexibility.

    I have tested this on my end and it seems to be working. You may need to relook at what is returned when you use get_terms as the array that is returned might be indexed in a different way to how the query arguments are recieved.

    EDIT

    Sorry I feel like I keep missing the initial question.

    $terms = get_terms( 'tm_portfolio_category' );
    

    is going to give you a list of terms.

    foreach ($terms as $term) {
        ?>
        List of categories
        <p>
            <a href="<?php echo $term->slug; ?>" title="Permanent Link to <?php echo $term->name ?>" ><?php echo $term->name ?></a>
        </p>
        <?php 
    }
    
    ?>
    

    Below that should give you the desired result without having to create another query.

Comments are closed.