Support

Account

Home Forums Front-end Issues Query multiple values in relationship field Reply To: Query multiple values in relationship field

  • You need to use LIKE to match products with recipes and you need to add an array for each product you’re trying to match. Also, remember to put the ID inside parentheses (“”) otherwise you will match “12” with “123”.

    // GET RECIPES THAT HAVE A RELATIONSHIP TO PRODUCTS ON THE CURRENT PAGE - manually
    $recipes_manual_meta_query = array(
    	'post_type' => 'recipes',
    	'meta_query' => array(
    		'relation' => 'OR',
    			array(
    				'key' => 'products_under_recipes',
    				'value' => '"' . $products[0]->ID . '"',
    				'compare' => 'LIKE'
    			),
    			array(
    				'key' => 'products_under_recipes',
    				'value' => '"' . $products[1]->ID . '"',
    				'compare' => 'LIKE'
    			)
    	)
    );
    

    This is the manual solution (without going over the array of products for page), to show you clearly what the meta_query should look like.