WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Dima
    Member
    Posted 2 years ago #

    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(); ?>
  2. Jerry
    Member
    Posted 2 years ago #

    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 :)

  3. Dima
    Member
    Posted 2 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic