0

I have a WP_Query to display a custom post type:

<?php

//* The Query
$obras_query = new WP_Query( array (
  'post_type' => 'obras_en_curso',
  'posts_per_page' => 4
) );

//* The Loop
if ( $obras_query->have_posts() ) { ?>

      <h3 class="title">Casas Unifamiliares - Obras en Curso</h3>

      <ul> <?php

    while ( $obras_query->have_posts() ): $obras_query->the_post(); ?>

        <li>
          <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li> <?php

    endwhile; ?>

      </ul> <?php

    //* Restore original Post Data
    wp_reset_postdata();

}
?>          

Now I have created a taxonomy called "casa_unifamiliar".

This taxonomy is hierarchical, it has 2 types of "sub-taxonomies", one called "curso" and one called "finalizada". How can I Query a post with the sub-taxonomy "curso".

1 Answer 1

2

You can create a taxonomy query. Assuming that "curso" is the term slug, it would look like this:

$query = new WP_Query( array(
'post_type' => 'obras_en_curso',
'posts_per_page' => 4,
'tax_query' => array(
    array(
        'taxonomy' => 'casa_unifamiliar',
        'field'    => 'slug',
        'terms'    => 'curso',
    ),
  ),
) );

For more information, see: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

1
  • Thank you, that works, I was missing the "terms" part on every attempt I was making.
    – Sergi
    Commented Feb 22, 2018 at 8:48

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.