0

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

5
  • I am pretty sure your example code won't work: $languages vs. $langues ...
    – kaiser
    Jun 19, 2014 at 9:55
  • It does. Ckeck again -> languages and langues are both different variables which have different purpouse : languages is an object with child terms and langues, an array with only the terms names. But it's not really helpfull for my question : How to know, in the langues array, which term is matching for the query. But thank you anyway ;)
    – Neovea
    Jun 19, 2014 at 9:58
  • Oh. You didn't initialize it before starting to fill it. Hint: That's a bad habbit. If there's nothing to set, then not even an empty array will be passed (the loop won't run on empty/non-existing elements) and your program will fail with a Fatal Error.
    – kaiser
    Jun 19, 2014 at 10:22
  • Also you are pushing elements to an array plus concatenating a string with .=. Just use = and you are good.
    – kaiser
    Jun 19, 2014 at 10:24
  • All right then, I corrected like this : $langues = array(); before foreach 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
    Jun 19, 2014 at 12:15

1 Answer 1

0

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.

1
  • This is empty. I edited my post to be more precise.
    – Neovea
    Jun 19, 2014 at 13:34

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.