Is there a way to get all the posts from a taxonomy in Wordpress ?
In taxonomy.php
, I have this code that gets the posts from the term related to the current term.
$current_query = $wp_query->query_vars;
query_posts( array( $current_query['taxonomy'] => $current_query['term'], 'showposts' => 10 ) );
I'd like to create a page with all the posts in the taxonomy, regardless of the term.
Is there a simple way to do this, or do I have to query the taxonomy for the terms, then loop trough them, etc.
Unlike for post types, WordPress does not have a route for the taxonomy slug itself.
To make the taxonomy slug itself list all posts that have any term of the taxonomy assigned, you need to use the
EXISTS
operator oftax_query
inWP_Query
:@PaBLoX made a very nice solution but I made a solution myself what is little tricky and doesn't need to query for all the posts every time for each of the terms. and what if there are more than one term assigned in a single post? Won't it render same post multiple times?
this function
m_explode
is a custom function which i put into thefunctions.php
file.UPDATE
We don't require this custom
m_explode
function.wp_list_pluck()
function does exactly the same. So we can simply replacem_explode
withwp_list_pluck()
(parameters would be same). DRY, right?While in the query loop for terms, you can collect all post references in an array and use that later in a new WP_Query.
With that you'd post the first item, yo can then create a
foreach
; loop:That way you'd list them, if you want to post all of them, -my solution- create a normal wordpress loop inside the foreach one, but it has to have something like:
I posted something very similar here.