0

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?

Thanks

2 Answers 2

0

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() ) :
    $the_query->the_post();
    // Show Posts ...
endwhile;

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

0

Try this:

$query = new WP_Query(array(
    'post_type'=>'courses',
    'nopaging'=>true,
    'tax_query'=> array(
        array('taxonomy'=>'courses_category',
        'field'=>'slug',
        '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.

4
  • i've experiment this one but it returns nothing.....incredible,
    – Rui Costa
    Commented Sep 19, 2017 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
    Commented Sep 19, 2017 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
    Commented Sep 20, 2017 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
    Commented Sep 24, 2017 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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