wp query - Get the tax term in which is a post via wp_query

admin2025-01-07  6

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

Share Improve this question edited Jun 19, 2014 at 14:34 kaiser 50.8k27 gold badges150 silver badges244 bronze badges asked Jun 19, 2014 at 8:27 NeoveaNeovea 1492 silver badges10 bronze badges 5
  • I am pretty sure your example code won't work: $languages vs. $langues ... – kaiser Commented 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 Commented 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 Commented 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 Commented 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 Commented Jun 19, 2014 at 12:15
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258938a551.html

最新回复(0)