$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Pass array of taxonomy terms to wp_query|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Pass array of taxonomy terms to wp_query

matteradmin8PV0评论

I am trying to compile an array of taxonomies and terms to pass to wp_query, my array looks like this...

Array
(
    [0] => Array
        (
            [taxonomy] => color
            [field] => term_id
            [terms] => Array
                (
                    [0] => 15
                )

        )

    [1] => Array
        (
            [taxonomy] => shape
            [field] => term_id
            [terms] => Array
                (
                    [0] => 87
                )

        )

    [2] => Array
        (
            [taxonomy] => weight
            [field] => term_id
            [terms] => Array
                (
                    [0] => 3
                    [1] => 54
                )

        )

    [3] => Array
        (
            [taxonomy] => rating
            [field] => term_id
            [terms] => Array
                (
                    [0] => 87
                    [1] => 88
                )

        )

)

I am trying to pass it to wp_query like this...

$args = array(
    'post_type' => 'post',
    'relation' => 'OR',
    'tax_query' => $myarray,
);
$query = new WP_Query( $args );

This is not giving me any results, where am I going wrong?

I am trying to compile an array of taxonomies and terms to pass to wp_query, my array looks like this...

Array
(
    [0] => Array
        (
            [taxonomy] => color
            [field] => term_id
            [terms] => Array
                (
                    [0] => 15
                )

        )

    [1] => Array
        (
            [taxonomy] => shape
            [field] => term_id
            [terms] => Array
                (
                    [0] => 87
                )

        )

    [2] => Array
        (
            [taxonomy] => weight
            [field] => term_id
            [terms] => Array
                (
                    [0] => 3
                    [1] => 54
                )

        )

    [3] => Array
        (
            [taxonomy] => rating
            [field] => term_id
            [terms] => Array
                (
                    [0] => 87
                    [1] => 88
                )

        )

)

I am trying to pass it to wp_query like this...

$args = array(
    'post_type' => 'post',
    'relation' => 'OR',
    'tax_query' => $myarray,
);
$query = new WP_Query( $args );

This is not giving me any results, where am I going wrong?

Share Improve this question asked Nov 12, 2018 at 15:03 fightstarr20fightstarr20 1,1358 gold badges26 silver badges47 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

The problem is ambiguity, you can't just put in 'relation' => 'OR' in the main parameter list, because how would WP_Query know if it's for the tax_query and not the meta_query?

For how it's supposed to work, and where the relation parameter goes we need to refer to the official docs for WP_Query which gives us this example:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'quotes' ),
        ),
        array(
            'taxonomy' => 'post_format',
            'field'    => 'slug',
            'terms'    => array( 'post-format-quote' ),
        ),
    ),
);
$query = new WP_Query( $args );

Setting it may be as simple as:

$myarray['relation'] = 'OR'; // untested
Post a comment

comment list (0)

  1. No comments so far