• Resolved ryanbowden

    (@ryanbowden)


    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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    No, don’t use strings! While maybe possible, it is a recipe for failure. Keep everything in array form. PHP has a rich set of array manipulation functions. You should find a way to do anything you want with arrays.

    I don’t think your approach is ideal, but it’ll work. To answer your question just FYI:
    First initialize the ‘tax_query’ array to any arguments that exist for all queries, at the very least

    \\ initialize
    $tax_query = array('relation' => 'AND',);

    Then loop through each term a visitor has selected and build a single argument array

    $arg = array(
            'taxonomy' => 'product_category',
            'field'    => 'slug',
            'terms'    => array( $category->slug,),
        );

    Add the argument to the main array, still in the loop

    //add another
    $tax_query[] = $arg;

    Finally, after the loop, build the main arguments array which includes the tax_query argument

    // lot's more argument array stuff before this
    'tax_query' => $tax_query,

    Is there a better way? It depends if the terms are in the same taxonomy or not. For different taxonomies, the above approach is correct. For same taxonomy terms, use the same concept for the terms loop array

    \\collect terms
    $terms[] = $category->slug;

    Or perhaps you have both situations. Combine the two concepts. Accumulate selected terms into different arrays depending on the taxonomy they belong to. Then build individual taxonomy argument arrays which are in turn used to build the main tax_query array.

    Thread Starter ryanbowden

    (@ryanbowden)

    Thank you so much!!

    You really helped me make this work.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_query args adding muitiple tax_querys’ is closed to new replies.