WordPress.org

Support

Support » Plugins and Hacks » Hacks » List all categories in a custom taxonomy and post types under each of those cat.

List all categories in a custom taxonomy and post types under each of those cat.

  • Hi all,

    I need to list all categories in a custom taxonomy and custom post types under each of those categories. Here is the code I wrote so far. It doesn’t work :). It displays the categories but posts. It displays categories and posts if I remove the cat_ID argument:

    <?php query_posts('orderby=title&order=ASC&taxonomy=job_categories&post_type=ci_jobs') ?>
    But it displays all posts under all categories, even if the post doesn’t belong to it.

    &cat= category id should work – why it doesn’t?

    Script:

    <?php $categories = get_categories('taxonomy=job_categories&post_type=ci_jobs'); ?>
               <?php foreach ($categories as $category) : ?>
                    <div class="job-cat"><?php echo $category->name; ?></div>
    
                       <?php query_posts('orderby=title&order=ASC&taxonomy=job_categories&post_type=ci_jobs&cat='.$category->cat_ID) ?>
    
    <ul>
                       <?php while(have_posts()): the_post(); ?>
    
    <li><a>'><?php the_title() ?></a></li>
                       <?php endwhile; ?>
                       </ul>
               <?php endforeach; ?>
               <?php wp_reset_query(); ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • Here’s the altered code for 3.3.1 🙂

    <?php
    $catArgs = array(
    			'taxonomy'=>'job_categories'
    			// post_type isn't a valid argument, no matter how you use it.
    			);
    $categories = get_categories('taxonomy=job_categories&post_type=ci_jobs'); ?>
     <?php foreach ($categories as $category) : ?>
    	  <div class="job-cat"><?php echo $category->name; ?></div>
    		<?php
            $postArgs = array(
    			'orderby' => 'title',
    			'order' => 'ASC',
    			'post_type'=>'ci_jobs',
    			'cat'=>$category->cat_ID,
    			'tax_query' => array(
    					array(
    						'taxonomy' => 'job_categories'
    					)
    				)
    			);
    		 query_posts($postArgs) ?>
    		<ul>
    			 <?php while(have_posts()): the_post(); ?>
                <li><a><?php the_title() ?></a></li>
                 <?php endwhile; ?>
    		</ul>
     <?php endforeach; ?>
     <?php wp_reset_query();

    When you said “taxonomy” my general assumption was you have already set a custom “taxonomy” with it’s own tags. However, in-case you were simply talking of the general taxonomy already installed, you can look at the “tags” parameters instead.

    Tag Parameters
    Taxonomy Parameters

    Hope I’ve been helpful, happy coding 🙂

    Thank you for your help. Yes, I registered a custom taxonomy (job_categories) and a custom post type (ci_jobs). By the way the code works with regular taxonomies just fine.

    Unfortunately this new code doesn’t list the posts at all. Maybe the problem lies somewhere else?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘List all categories in a custom taxonomy and post types under each of those cat.’ is closed to new replies.
Skip to toolbar