I have a custom query which gets all the custom taxonomies for the custom post type and displays the latest post in each. At the moment it pulls in all the taxonomies. Is there a way I can limit the number of taxonomies displayed to a set number per page and paginate them?
// Set post type
$post_type = 'prints';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Get every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
// Query to show first post in taxonomy
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=1" );
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?><!--
--><div>
<a href="<?php esc_url( the_permalink() ); ?>">
<?php the_post_thumbnail ('medium'); ?>
<h2><?php echo $term->name; ?></h2>
</a>
</div><?php
endwhile; endif;
endforeach;
endforeach;
As discussed in chat and as @Rarst already told you, there’s no default WordPress way to accomplish
In fact there’s no way to accomplish
in WordPress. Simply because WordPress doesn’t do that – without custom SQL queries.
But there’re some things you can do to simplify your approach.
As you stated the following in chat, I’ll get a little bit more into detail:
Note: If you set the second argument of
get_object_taxonomies()
toobjects
, you’ll get the complete objects for later usage.And
get_terms()
takes an array of taxonomies as first argument. So you can do the following to get all terms of all your taxonomies that are assigned to theprints
CPT.Note: If you use the second argument above and got the full objects, use
wp_list_pluck()
to extract the taxonomy names.If you need to influence the order, you can use the
orderby
(default:name
) andorder
(default:ASC
endending) arguments in the second argument/array of the function call or use a filter:As stated in the beginning, there’s no default way to accomplish your target of getting a single post along the terms query. But you can still build your own SQL query. And there’s a filter for that:
So you can jump in, alter the query, include the posts table, etc.
The result of above filter callback will alter the query build by WordPress. The default in core looks like this:
The
JOIN
clause is built like the following:What you’ll have to do now is to
JOIN
the$wpdb->term_relationship
likely with something like the following:Borrowed from
wp_get_object_terms()
in core.The
WHERE
clause will then look likely like this:From here on you should be able to
JOIN
the$wpdb->posts
table on theID
and query one post per taxonomy term as well.I’d highly recommend to somehow cache the result, add it as transient or something similar as the query can get quite slow.
To make pagination work you shoul pass a paged query var. Maybe in a custom page template.
then…
Note: code posted above paginate taxonomies not terms, because you ask for that in question.