xAndyx
xAndyx

Reputation: 45

Wordpress search multiple tag combinations

Im trying to search posts using tag-combinations you can make in my search form. But i cant get the combinations to work.

The (checkbox) options;
Colors: red, blue, black
Shapes: round, square, diamond

I want to display posts that have these tag-combinations:
red AND round
OR
red AND square

I tried this but they doesn't seem to work:
$query = new WP_Query( 'tag=red+round,red+square' );

Wordpress sees this as 'tag=red,round,red,square'.

Anyone with a tip on how i can get this to work or maybe an alternative route?

Upvotes: 2

Views: 2820

Answers (1)

Dharmang
Dharmang

Reputation: 3028

Looks like shorthand version of WP_Query will not work in this case, try below parameters for WP_Query, detailed information is available here: WP_Query on WP Codex

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'post_tag',
            'field'    => 'slug',
            'terms'    => array( 'red', 'round' ),
            'operator' => 'AND',
        ),
        array(
            'taxonomy' => 'post_tag',
            'field'    => 'slug',
            'terms'    => array( 'red', 'square' ),
            'operator' => 'AND',
        ),
    ),
);
$query = new WP_Query( $args );

Upvotes: 1

Related Questions