1

I would like to display one post (oldest, but that's not that important for now) out of each of the custom category except one (called 'home').

So far I got to

1) display each custom taxonomy slug existing except one that I don't need.
2) display all custom posts of a certain taxonomy ('portraits')

How do I do the third and main part of the script - display one post from each of the custom taxonomy?

  //get all custom categories (taxonomy)
$terms = get_terms(array('taxonomy' => 'album', 'exclude' => 4)); 
foreach ($terms as $term){
    echo $term->slug; 
    echo "<br/>"; 
}
//get all custom post types of custom category "portraits"
//however, need to get one oldest post of each custom category from above
    $args = array(
    'post_type' => 'simple_image', 
    'posts_per_page' => '-1',
    'order_by' => 'date', 
    'order' => 'ASC', 
    'tax_query' => array(
        array(
            'taxonomy' => 'album',
            'field' => 'slug',
            'terms' => 'portraits' 
        )
    )
);

 $new_query = new WP_Query ($args);
  while ( $new_query->have_posts() ) : $new_query->the_post();    
         the_post_thumbnail('thumbnail') ; 
         endwhile; 

And why I am doing it - it's a photo gallery theme and I want to display a list of albums with a thumbnail. The thumbnail will come from one of the posts in the album.

[edit] I've seen a script where WP query was put in the foreach loop, that doesn't make much sense to me. Is there something simpler? Isn't there JOIN operation?

  • Hi, how many categories have you got? If there's a lot of them, then querying 1 post from each category is going to be expensive. – Jack Johansson May 19 '17 at 20:54
  • the number of categories is not known and will be different for each installation of the theme, but I don't expect it to be in the hundreds. More like below 100 – Natalia May 19 '17 at 20:55
0

You can use tax_query to query your posts based on the taxonomy. But be advised, if you have a lot of categories, then this is gonna take a lot of resources, unless you use a cache plugin.

$terms = get_terms(array('taxonomy' => 'album', 'exclude' => 4)); 
foreach ($terms as $term){
    $args = array(
    'post_type' => 'simple_image', 
    'posts_per_page' => '1',
    'order_by' => 'date', 
    'order' => 'ASC', 
    'tax_query' => array(
        array(
            'taxonomy' => 'album',
            'field' => 'slug',
            'terms' => $term->slug, 
            )
        )
    );
    $new_query = new WP_Query ($args);
    if ($new_query->have_posts()) {
        while($new_query->have_posts()){
            $new_query->the_post();
            the_post_thumbnail('thumbnail');
        }
    }
    wp_reset_postdata();
}

As you have already noticed, this will run 100 queries, if you have 100 terms under that taxonomy. So, make sure you use it in conjunction with a cache plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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