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
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
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
.
$languages
vs.$langues
... – kaiser Commented Jun 19, 2014 at 9:55Fatal Error
. – kaiser Commented Jun 19, 2014 at 10:22.=
. Just use=
and you are good. – kaiser Commented Jun 19, 2014 at 10:24$langues = array();
beforeforeach
and$langues[] = $tmp['slug'];
in the loop. Thank you for the advise. Indeed, I feel like I haven't been clear about what I wanted :$langues
returns an array with the different language terms. That's just fine if I want to give as an argument that array and make the query to return the matching posts. The fact is that there's only one term in this array which is matching. And I want to return it dynamicaly. Is there a way to do that ? – Neovea Commented Jun 19, 2014 at 12:15