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.

I am trying to list at least the last three updated Custom Post Type's title lets call it "Cast" inside another loop which is listed based on the Taxonomy terms called "Actress" I used the following loop to get the list of my tax terms:

$taxonomy = 'actress';
$term_args=array(
  'hide_empty' => false,
  'orderby' => 'name',
  'order' => 'ASC'
);
$tax_terms = get_terms($taxonomy,$term_args);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . $tax_term->name.'</li>';
}
?>
</ul>
<?php 

well this helped me to list the terms but I am really confused how to add the list of each terms associated custom post? to be more clear I illustrate following image,hopefully this will give you an idea what I am seeking for? Can you please help me to figure out how to do this? Thanks

Update

<?php

get_header();
//list terms in a given taxonomy
$taxonomy = 'actress';
$term_args=array(
  'hide_empty' => false,
  'orderby' => 'name',
  'order' => 'ASC'
);
$tax_terms = get_terms($taxonomy,$term_args);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
$loop = new WP_Query(
                  array(
                    'post_type' => 'cast',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'actress',
                            'field'=>'id', 
                            'terms' => $tax_term->ID
                           )
                    ),
                    'posts_per_page' => 3
                  )
             );
while ( $loop->have_posts() ) {
        $loop->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }

}

?>
</ul>
<?php get_footer(); ?>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Retrieval of multiple posts in WordPress is almost always handled by WP_Query class (or get_posts() function that wraps it).

There is quite a lot of documentation and information to it, but to narrowing it down to your specific case you will need to:

  • set up posts loop for each term
  • pass as arguments to each loop:
    • your post type
    • desired number of posts
    • current term you wish to fetch posts for (see "taxonomy parameters" section in docs)
share|improve this answer
    
Hi Rarst, Thanks for your complete hints but to be honest I a little bit( let's say a lot :-) confused here! I almost know hoe to get Custom Post Types based on one taxonomy term (please take a look at update code above) but I am not sure how to integrate this post with listed terms! –  Mona Coder Nov 15 '13 at 10:11
    
@MonaCoder you pretty much have it nailed, you just need to pass term information from your loop with terms to querying posts, something like 'field'=>'id', 'terms' => $tax_term->ID in taxonomy query. –  Rarst Nov 15 '13 at 10:39
    
@ Rarst, I tried this a lot and ended up with above, again, updated code. Still not working, I mean I am only getting the list of terms and not Post Titles under each term! Can you please let me know what I am doing wrong? –  Mona Coder Nov 15 '13 at 18:51
    
@MonaCoder move WP_Query inside your foreach loop (since you want to run it for every term) and see very first example of usage in docs for how to output titles using it. –  Rarst Nov 15 '13 at 18:55
    
OMG! this is gonna kill me!, I updated the code as above, No Errors! but I am not getting any of the lists! Not Terms neither CPTs –  Mona Coder Nov 15 '13 at 19:21

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.