Wordpress: Query a custom taxonomy


/ Published in: PHP
Save to your folder(s)

Use WP_Query to query a custom taxonomy, using the tax_query parameter.


Copy this code and paste it in your HTML
  1. <?php
  2. // Example query to select all posts belonging to a custom taxonomy.
  3. $args = array(
  4. 'tax_query' => array(
  5. 'taxonomy' => 'TAXONOMY_NAME',
  6. 'field' => 'slug',
  7. 'terms' => 'TAXONOMY_SLUG'
  8. )
  9. )
  10. );
  11. $the_query = new WP_Query( $args );
  12.  
  13. // The Loop
  14. while ( $the_query->have_posts() ) : $the_query->the_post();
  15. echo '<li>';
  16. the_title();
  17. echo '</li>';
  18. endwhile;
  19.  
  20. // Reset Post Data
  21. wp_reset_postdata();
  22. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.