2

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 :)

2

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
];
  • 1
    Don't have PHP5.4+ so I solved: $term_ids = get_terms( 'authors', 'fields=ids' ); // Only get term ids 'tax_query' => array( array( 'taxonomy' => 'authors', 'terms' => $term_ids ) ) Thank you @PieterGoosen – Elena Jul 23 '15 at 16:55
  • Great. You should consider upgrading your PHP version. VERY IMPORTANT NOTE: All versions before PHP 5.4 is not supported anymore, which can cause huge security issues. ;-) – Pieter Goosen Jul 23 '15 at 16:57
  • I just checked and I have an older version (5.3). I will update soon my PHP version! ;) – Elena Jul 23 '15 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.