1

In woocommerce i want product by brand (ORDER BY A-Z) with specific custom taxonomy. I have two taxonomies product_cat and product_brand. I did some code to reach my goals but the problem is i have to call WP_Query again and again. The code which i tried:-

$args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC'
    );
    $terms_brand=get_terms('product_brand',$args);
    foreach($terms_brand as $term_b){
    $args = array(
        'post_type' => 'product',
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'term_id',
                'terms'    => $term->term_id,
            )

        ),
        'product_brand' => $term_b->slug
    );
    $query=query_posts($args);
    while(have_posts()){
    the_post();
    $terms = get_the_terms( get_the_ID(), 'product_brand' );
    echo the_title_attribute().' --- '.$terms[0]->name.'<br/>';

    }
}

How could i make it possible not to run WP_Query continuously.

1

You're close, syntax is just slightly off. I can't quite work out how/what terms you need to query for, but this should be the structure of your arguments:

$query = new WP_Query(
    array(
        'post_type' => 'product',
        'orderby'   => 'name',
        'order'     => 'ASC',
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'term_id',
                'terms'    => $ids_of_product_cat_terms,
            ),

            array(
                'taxonomy' => 'product_brand',
                'field'    => 'term_id',
                'terms'    => $ids_of_product_brand_terms,
            ),
        ),
    )
);

Read up on tax queries from the codex.

  • thanks for your answer. But this doesn't produce a desirable result because its order product by name instead of order by product by brand name. – Vipin Sep 11 '15 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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