14

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.

Andrei
  • 1,606
  • 4
  • 19
  • 31

5 Answers5

16

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

 <?php
     $taxonomy = 'my_taxonomy'; // this is the name of the taxonomy
     $terms = get_terms($taxonomy);
     $args = array(
        'post_type' => 'post',
        'tax_query' => array(
                    array(
                        'taxonomy' => 'updates',
                        'field' => 'slug',
                        'terms' => wp_list_pluck($terms,'slug')
                    )
                )
        );

     $my_query = new WP_Query( $args );
     if($my_query->have_posts()) :
         while ($my_query->have_posts()) : $my_query->the_post();

              // do what you want to do with the queried posts

          endwhile;
     endif;
  ?>

wp_list_pluck

Obed Parlapiano
  • 3,226
  • 3
  • 21
  • 39
maksbd19
  • 3,785
  • 28
  • 37
12
$myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');    
echo  $myterms[0]->name;

With that you'd post the first item, yo can then create a foreach; loop:

foreach ($myterms as $term) { ?>
    <li><a href="<?php echo $term->slug; ?>"><?php echo $term->name; ?></a></li> <?php
} ?>

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:

foreach ($myterms as $term) :

$args = array(
    'tax_query' => array(
        array(
            $term->slug
        )
    )
);

//  assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);

// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();

the_title();
blabla....

endwhile;

endforeach;

I posted something very similar here.

Community
  • 1
  • 1
  • 3
    The above example on the line with `'taxonomy' => '$term_name'` needs to be double-quoted like this `'taxonomy' => "$term_name"`, or better no quotes like this `'taxonomy' => $term_name`, or even better omit the prior assignment and just use `'taxonomy' => $term->slug`. That said, the method shown [has been deprecated in favor of](http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters) using `'tax_query' => array(...)`. Hope this helps. – MikeSchinkel May 15 '13 at 00:43
  • Sorry, the delay... you are right. I modified my answer accordingly :) – Pablo Olmos de Aguilera C. Jun 22 '13 at 22:45
  • Excellent! Together I hope our efforts help others. – MikeSchinkel Jun 23 '13 at 04:37
  • I don't quite know why, but in order to get this snippet to work I had to remove the WordPress Loop section from the `foreach` loop. It seemed as if every time the `foreach` iterated through, it created a duplicate section of posts. To give an example, if I have three posts under three different terms and I wanted to display them all on one page and I used this snippet near out of the box, it would display the three posts as it should, but it would then duplicate the three posts two more times after the first, according to the number of terms I have in my taxonomy.@pablox @MikeSchinkel – ExcellentSP Apr 19 '15 at 22:03
  • Well, I made up that almost 5 years ago. I barely remember something about that, and also, Wordpress has been ran into a lot of API changes and major versiones updates... sorry for not being more helpful =( – Pablo Olmos de Aguilera C. Apr 26 '15 at 00:50
2

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 of tax_query in WP_Query:

// Register a taxonomy 'location' with slug '/location'.
register_taxonomy('location', ['post'], [
  'labels' => [
    'name' => _x('Locations', 'taxonomy', 'mydomain'),
    'singular_name' => _x('Location', 'taxonomy', 'mydomain'),
    'add_new_item' => _x('Add New Location', 'taxonomy', 'mydomain'),
  ],
  'public' => TRUE,
  'query_var' => TRUE,
  'rewrite' => [
    'slug' => 'location',
  ],
]);

// Register the path '/location' as a known route.
add_rewrite_rule('^location/?$', 'index.php?taxonomy=location', 'top');

// Use the EXISTS operator to find all posts that are
// associated with any term of the taxonomy.
add_action('pre_get_posts', 'pre_get_posts');
function pre_get_posts(\WP_Query $query) {
  if (is_admin()) {
    return;
  }
  if ($query->is_main_query() && $query->query === ['taxonomy' => 'location']) {
    $query->set('tax_query', [
      [   
        'taxonomy' => 'location',
        'operator' => 'EXISTS',
      ],  
    ]);
    // Announce this custom route as a taxonomy listing page
    // to the theme layer.
    $query->is_front_page = FALSE;
    $query->is_home = FALSE;
    $query->is_tax = TRUE;
    $query->is_archive = TRUE;
  }
}
sun
  • 991
  • 9
  • 13
1
<?php
get_posts(array(
    'post_type' => 'gallery',
    'tax_query' => array(
        array(
        'taxonomy' => 'gallery_cat',
        'field' => 'term_id',
        'terms' => 45)
    ))
);
?>
0

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.

$post__in = array(); 
while ( $terms_query->have_posts() ) : $terms_query->the_post();
    // Collect posts by reference for each term
    $post__in[] = get_the_ID();
endwhile;

...

$args = array();
$args['post__in'] = $post__in;
$args['orderby'] = 'post__in';
$other_query = new WP_Query( $args );
Jens
  • 1