How to filter WooCommerce products by custom attribute

user1974752 picture user1974752 · Feb 28, 2013 · Viewed 99.9k times · Source

I'm trying to filter WooCommerce product archive by custom attributes.

For example, there are 5 products with attribute "color" containing "red", and another 3 with attribute "color" containing "blue".

How can I apply a filter to the products loop, so only the products containing "red" will be shown?

Thanks

Answer

jnhghy - Alexandru Jantea picture jnhghy - Alexandru Jantea · Feb 28, 2013

On one of my sites I had to make a custom search by a lot of data some of it from custom fields here is how my $args look like for one of the options:

$args = array(
    'meta_query' => $meta_query,
    'tax_query' => array(
        $query_tax
    ),
    'posts_per_page' => 10,
    'post_type' => 'ad_listing',
    'orderby' => $orderby,
    'order' => $order,
    'paged' => $paged
);

where "$meta_query" is:

$key = "your_custom_key"; //custom_color for example
$value = "blue";//or red or any color
$query_color = array('key' => $key, 'value' => $value);
$meta_query[] = $query_color;

and after that:

query_posts($args);

so you would probably get more info here: http://codex.wordpress.org/Class_Reference/WP_Query and you can search for "meta_query" in the page to get to the info