Home Forums Front-end Issues Cross-filtering by meta value


Cross-filtering by meta value

  • Hi,

    I have a custom post type named “product”, and another one named “category”.
    A product can belong to one OR several categories, and i’d like to be able to get only products belonging to the categories specified, not with an OR, but with an AND.
    Example :

    • product A belongs to categories X and Y
    • product B belongs to categories Y and Z
    • product C belongs to category Z

    If i ask “category X” (by its ID), i should get only product A.
    If i ask “category Y”, i should get product A and product B.
    If i ask “category Z”, i should get product B and product C.
    But if i ask “X,Y”, i should get only product A, which is the only one belonging to both categories X and Y.

    Any help ?

  • Hi,

    I found my answer :

    • field values must be meta queried one at a time (in the query definition), because you should try any order, and it could be mess with as less as 3 values
    • The basic query is on this model, section 3 (Multiple custom field values), since the field queried is a post, with potential multiple values
    • The relation is a “AND” in order to have the meta field like value1 and like value 2 and llike value3
    $values = explode(",", $values_str);
    $query_values = [];
    foreach($values as $value) {
    	$query_values[] = [
    		'key'		=> 'produit_categories',
    		'value'		=> $value,
    		'compare'	=> 'LIKE'
    $args['meta_query']	=
    		'relation'		=> 'AND',
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Cross-filtering by meta value’ is closed to new replies.