Get the tax term in which is a post via wp_query

I'll rewrite my original question because I feel like I can't be understood : I made a template page on which I have different WP_Query. I have a category taxonomy (countries). This template page matches with one of this tax's terms.

For example, the page France matches with the category tax term

$countries-france

The administrator have the ability, if he wants, to create a translation which will be seen on the same page template.

Those translated posts will be categorized with the same cat tag $countries-france, but with also another one (for e.g.):

$language-english

What I'd like to do now is to check if there is a matching post with both france and english cat terms, and if yes, return the language term (english) or any other one if it does exist.

For example, I have this query:

// Get the language cat term ID
$parent_id = get_cat_ID( 'languages' );
// Get the child terms of language
$languages = get_term_children( $parent_id, 'category' );
// Put them in an array for the query
$langs = array();
foreach ( $languages as $key = $value ) {
    $cat = get_category( $value, ARRAY_A );
    $langs[] = $cat['slug'];
}
// The Query
$langQuery = new WP_Query( array(
    'post_type' = 'post',
    'tax_query' = array(
        'relation' = 'AND',
        array(
            'taxonomy'     = 'category',
            'category__in' = array( 'countries' ),
            'parent'       = '1',
            'field'        = 'slug',
            'terms'        = array( $cat[0] ) // The page's slug
        ),
        array(
            'taxonomy'     = 'category',
            'category__in' = array( 'languages' ),
            'parent'       = '1',
            'field'        = 'slug',
            'terms'        = $langs
        )
    )
) );

Thanks for helping

Topic tax-query wp-query taxonomy Wordpress

Category Web


When you inspect your SQL query right after you executed it

$GLOBALS['wp_query']->last_query

you will see that the SQL statement is asking for AND. Jump into the posts_where or the the posts_clauses filter (where you need to look up the where keys value) and alter it to OR.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.