WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Custom Post Type = 'projects' with a Hierarchical Taxonomy = 'projects_category'

- big-corporates
-- company one
---- first project 1
---- second project 1
---- third project 1
-- company two
---- first project 2
---- second project 2
-- company three
---- first project 3
---- second project 3
---- third project 3
- small-businesses
-- company four
---- first project 4
---- etc...
-- company five
-- company six

Below I get a list of all projects listed in 'projects_category' under 'big-corporates' whereas I want to limit the list to just one project from each subcategory of 'big-corporates'.

    global $post;
    $args = array( 'post_type' => 'projects', 'projects_category' => 'big-corporates' );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post); ?>
        <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php #the_title(); ?><?php $terms = get_the_term_list( $post->ID, 'projects_category' ); $terms = strip_tags( $terms ); echo $terms; ?></a></li>
    <?php endforeach; ?>

I am trying to achieve a list like this:

company one (links to first project 1)
company two (links to first project 2)
company three (links to first project 3)

How can I do that?

share|improve this question

I'm not sure this is the best way but it works:

//First get all child categories if 'big_corporates'
$categories = $args = array(
    'parent'                   => $big_corporates_ID,
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'taxonomy'                 => 'projects_category'

//then loop over them and get the first post of each:
echo '<ul>';
foreach ($categories as $category) {
    $projects = get_posts(array('numberposts' => 1, 'post_type' => 'projects', 'projects_category' => $category->slug));
    foreach( $projects as $project ) {
        ?><li><?php echo $category->name; ?> - <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php
echo '</ul>';
share|improve this answer
Hey, thanks for trying, but its not working for me. It just repeats the same first post through each iteration. I solved the problem by organising the project a different way. – Andy Jul 12 '11 at 19:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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