Get all posts from custom taxonomy in Wordpress Get all posts from custom taxonomy in Wordpress wordpress wordpress

Get all posts from custom taxonomy in Wordpress


@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, 'orderby=count&hide_empty=1' ); // for more details refer to codex please.     $args = array(        'post_type' => 'post',        'tax_query' => array(                    array(                        'taxonomy' => 'updates',                        'field' => 'slug',                        'terms' => m_explode($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;  ?>

this function m_explode is a custom function which i put into the functions.php file.

    function m_explode(array $array,$key = ''){             if( !is_array($array) or $key == '')             return;                $output = array();        foreach( $array as $v ){                    if( !is_object($v) ){                return;            }            $output[] = $v->$key;        }        return $output;      }

UPDATE

We don't require this custom m_explode function. wp_list_pluck() function does exactly the same. So we can simply replace m_explode with wp_list_pluck() (parameters would be same). DRY, right?


$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 loopglobal $wp_query;$wp_query = new WP_Query($args);// starting loopwhile ($wp_query->have_posts()) : $wp_query->the_post();the_title();blabla....endwhile;endforeach;

I posted something very similar here.


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;  }}