Support

Account

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

  • Now it’s just a matter of php-array trickery to generate this query from array.

    // GET RECIPES THAT HAVE A RELATIONSHIP TO PRODUCTS ON THE CURRENT PAGE - dynamically
    $recipes_dynamic_meta_query = array(
    	'post_type' => 'recipes',
    	'meta_query' => array(
    		'relation' => 'OR'
    	)
    );
    
    foreach($product_ids as $product_id) {
    	array_push($recipes_dynamic_meta_query['meta_query'], array(
    		'key' => 'products_under_recipes',
    		'value' => '"' . $product_id . '"',
    		'compare' => 'LIKE'
    	));
    }
    

    Run this string trough get_posts and you’re set!

    $recipes_manual = get_posts($recipes_dynamic_meta_query);