Get pots by mixed taxonomies and include results from the taxonomy’s children if exist

I have been able to filter posts from a custom post type (ad) filtered by two taxonomies

$myposts = get_posts( array (
        'posts_per_page'   => $ipp,
        'paged'     =>  $current_page,

        'post_type' => 'ad',
        'post_status' => 'publish',
        'tax_query' => $tax_params,
     )
);

Where $tax_params:

$tax_params = array(
    'relation' => 'AND',
    $area_params, 
    $category_params
);

And $area_params and ‘$category_params`:

$area_slug = $params['area_slug'];
$category_slug = $params['category_slug'];
if ( !empty ( $category_slug ) ) {
    $category_params = array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => array("$category_slug")
    );  
} else {
    $category_params = array();
}
if ( !empty ( $area_slug ) ) {
    /* Ver si el area tiene hijos */
    $area_params = array(
        'taxonomy' => 'area',
        'field' => 'slug',
        'terms' => array("$area_slug")
    );  
} else {
    $area_params = array();
}

the thing is than when the filter is applied to a category that has childrens (parent!=0), the children posts results wont appear,

How Can modify the tax_query to include also the results for the taxonomie childrens?

Read more here: Get pots by mixed taxonomies and include results from the taxonomy’s children if exist

Leave a Reply

Your email address will not be published. Required fields are marked *