1

i am creating a custom search system which search my custom post type recipes, in the search it searches the taxonomies terms that people have selected.

Now due to the way it can hard code all these i need to build the args by a loop of some sort.

Here a code example if what i have,

$args = array(
'post_type'      => get_post_type(),
'orderby'        => 'menu_order',
'order'          =>  'ASC',
'posts_per_page' => -1,
'no_found_rows'  => true,
'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'product_category',
        'field'    => 'slug',
        'terms'    => array($category->slug), //Supply the current product_category terms
    ),
    array(
        'taxonomy' => 'product_locations',
        'field'    => 'slug',
        'terms'    => array($current_location), //Add the product_locations term
        'operator' => 'IN',
    ),
),
);

What i need is to add:

array(
        'taxonomy' => 'product_category',
        'field'    => 'slug',
        'terms'    => array($category->slug), //Supply the current product_category terms
    ),

What i need is to add them programmatically when a person selects i have tried build the args array with loops and adding strings together but i can't get it to work. Does anyone have any quick way it can be done?

1 Answer 1

0

You could use IF conditions to add the parameters,

$args = array(
'post_type'      => get_post_type(),
'orderby'        => 'menu_order',
'order'          =>  'ASC',
'posts_per_page' => -1,
'no_found_rows'  => true,
'tax_query' => array(),
);

if(condition){
    $args['tax_query'] = array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_category',
            'field'    => 'slug',
            'terms'    => array($category->slug), //Supply the current product_category terms
        ),
        array(
            'taxonomy' => 'product_locations',
            'field'    => 'slug',
            'terms'    => array($current_location), //Add the product_locations term
            'operator' => 'IN',
        ),
    ),
}
else{
     $args['tax_query'] = array(
        array(
            'taxonomy' => 'product_category',
            'field'    => 'slug',
            'terms'    => array($category->slug), //Supply the current product_category terms
        ),
     );
}

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.