I've created a page with list of custom terms: enter image description here

Under each Term, i need to show every posts with post_type = 'courses'e taxnonomy = 'courses_category'

What will be the query to display those records?



You will need to use this code:

$the_query = new WP_Query( array(
'post_type' => 'courses',
'tax_query' => array(
    array (
        'taxonomy' => 'courses_category',
        'field' => 'slug',
        'terms' => 'yourterm'

    ) );

while ( $the_query->have_posts() ) :
    // Show Posts ...

You can convert this in shortcode and just append it on your page trough your wordpress content editor or just insert this where you want to show your posts.

Check Codex or this link

| improve this answer | |

Try this:

$query = new WP_Query(array(
    'tax_query'=> array(
        'terms'=>'recursos-humanos'//replace this with the term slugs

I would probably use three queries for the three terms you have above (changing the 'terms' value with the slug of the taxonomy term. You could get fancy and link all three terms together with a single query ordered by post type.

| improve this answer | |
  • i've experiment this one but it returns nothing.....incredible, – Rui Costa Sep 19 '17 at 15:38
  • @RuiCosta Strange. Make sure the tax_query is a nested array (array of an array) and double check the slug of the taxonomy term. – jdp Sep 19 '17 at 16:44
  • This is the nested array Array ( [post_type] => courses [nopaging] => 1 [tax_query] => Array ( [0] => Array ( [taxonomy] => courses_category [field] => slug [terms] => recursos-humanos ) ) ) – Rui Costa Sep 20 '17 at 21:36
  • @RuiCosta Did you register this taxonomy when you registered the custom post type? Refer to the register_post_type() function developer.wordpress.org/reference/functions/register_post_type – jdp Sep 24 '17 at 15:36

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.