Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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'

        );

share|improve this question
    
you mean something like $args['tax_query'][] = array(... ? –  Bainternet Dec 27 '12 at 11:09

1 Answer 1

/**
 * 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' );
share|improve this answer
    
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 '12 at 8:03
    
It has been solved with custom query...... –  Khan Dec 28 '12 at 7:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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