0

I want to run a query base on the following $args. My question that how to pass variable number of arrays to tax_query as currently i have two arrays within tax_query it can be 1 , 3 or 5 and so on. Is there any possibility ?

$args = array (
            'posts_per_page' => 10,
            'tax_query' => array(
                'relation' => 'OR',
                array(
                    'taxonomy' => $Tax[0],
                    'field' => 'slug',
                    'terms' => array( $Tag )
                ),
                array(
                    'taxonomy' => $Tax[1],
                    'field' => 'slug',
                    'terms' => array( $Tag )

                )
            ),
            'order' => 'DESC'

        );

1
  • you mean something like $args['tax_query'][] = array(... ?
    – Bainternet
    Dec 27, 2012 at 11:09

2 Answers 2

0
/**
 * Define a new function that uses $args and wp_parse_args()
 */
function explain_parse_args( $args ) {
    $defaults = array (
        'text' => 'wp_parse_args() merges $args into $defaults',
        'before' => "

", 'after' => "

\n", 'echo' => TRUE ); // Parse incomming $args into an array and merge it with $defaults $args = wp_parse_args( $args, $defaults ); // OPTIONAL: Declare each item in $args as its own variable i.e. $type, $before. extract( $args, EXTR_SKIP ); $output = $before . $text . $after; if (!$echo) return $output; echo $output; } /** * Run our new function using the defaults (no $args) * This would print out: *

wp_parse_args() merges $args into $defaults

*/ explain_parse_args(); /** * Run the function with some options overriden with an array * This would echo the output with the modified text and before arguments: * A better explanation

*/ explain_parse_args( array ( 'text' => "A better explanation", 'before' => "" ) ); /** * We can also pass in URL-style string-query and it will be converted * This would set $args['echo'] to 1 and $args['text'] to 0 */ explain_parse_args( 'echo=1&text=0' );
2
  • I think you have not got my question I am just asking how to add one or more array to tax_query dynamically. Currently there are two array one is array( 'taxonomy' => $Tax[1], 'field' => 'slug', 'terms' => array( $Tag ) ) I want to add as many as i like but dynamically.
    – Khan
    Dec 27, 2012 at 8:03
  • It has been solved with custom query......
    – Khan
    Dec 28, 2012 at 7:16
0

Here's how I'd do it:

$args = array (
  'posts_per_page' => 10,
  'order' => 'DESC'
);
$tax_query = array('relation' => 'OR');

for($i=0;$i<count($Tax),$i++){
  array_push($tax_query, array(
    'taxonomy' => $Tax[$i],
    'field' => 'slug',
    'terms' => array( $Tag )
  ));
}

$args['tax_query'] = $tax_query;

Using shorthand array-braces might be more legible:

$args = [
  'posts_per_page' => 10,
  'order' => 'DESC'
];

$tax_query = ['relation' => 'OR'];

for($i=0;$i<count($Tax),$i++){
  $tax_query[] = [
    'taxonomy' => $Tax[$i],
    'field' => 'slug',
    'terms' => array( $Tag )  
  ];
}

$args['tax_query'] = $tax_query;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.