Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

projects
- 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'.

<?php
    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

1 Answer 1

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 ) {
        setup_postdata($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

 
discard

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.