Query custom taxonomy for category including children

tax_query is used to get the posts associated with certain taxonomy.

  • {tax} (string) – use taxonomy slug. Deprecated as of Version 3.1 in favor of ‘tax_query’.
  • tax_query (array) – use taxonomy parameters (available with Version 3.1).
    • taxonomy (string) – Taxonomy.
    • field (string) – Select taxonomy term by (‘id’ or ‘slug’)
    • terms (int/string/array) – Taxonomy term(s).
    • include_children (boolean) – Whether or not to include children for hierarchical taxonomies. Defaults to true.
    • operator (string) – Operator to test. Possible values are ‘IN’, ‘NOT IN’, ‘AND’.
$args = array(
    'post_type' => 'event',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'event-categories',
            'field' => 'id',
            'terms' => 43
        )
     )
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post;
        // do something
    }
}