Get and loop posts with all taxonomy terms

I created a page that displays the posts orderby comment_count. I have a custom taxonomy named 'authors'.

I would like to display only the posts with taxonomy values. Not a specific term but simply IF the post HAS A TERM from 'authors' then display that post.

Do you think it's possible? Here's my code:

<?php $args = array (
     'orderby' => 'comment_count',
     'posts_per_page' => -1,
); ?>

<?php $popular = new WP_Query($args); ?>
<?php while ($popular->have_posts()) : $popular->the_post(); ?>

     <article class="item-list item-list-custom <?php echo'item_'.$count; ?>">
         <h2 class="post-title">
            <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a>
         </h2>
     </article>

<?php endwhile; ?>

I need something like:

$args = array(
    'posts_per_page' => 10,
    'orderby' => 'comment_count',

    'tax_query' => array(
    array(
        'taxonomy' => 'authors',
        'field'    => 'slug',
        'terms'    => ALL THE TERMS
    ),
 )
);

I hope everything's clear.. sorry for my english :)

Answers 1

  • Your best option will be to get an array of term ids by using get_terms which belongs to the authors taxonomy and then using that as your array of terms in your tax_query

    The following requires PHP5.4+

    $term_ids = get_terms( 'authors', ['fields' => 'ids'] ); // Only get term ids 
    $args = [
        'tax_query' => [
            [
                'taxonomy' => 'authors',
                'terms' => $term_ids,
            ]
        ],
        // Rest of your arguments
    ];
    

Related Questions