Support

Account

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

  • Hi,

    you have to write a complex meta_query to get this working. I’ll try to make it clear by explaining it in steps.

    First, I’ve rewritten your code a bit to eliminate french words, just so it’s obvious what we’re doing. I’m on the “Work” page here. This is the setup before we get into code:

    CUSTOM POST TYPE Products
    – Product 1
    – Product 2
    – Product 3
    CUSTOM POST TYPE Recipes
    – Recipe 1 (has Product 2 an Product 3 linked)
    – Recipe 2 (has Product 2 linked)
    – Recipe 3 (has Product 1 and Product 2 linked)
    PAGE Work
    – has Product 1 and Product 3 linked

    … now you want to display all the Recipes that are shared between the current page (Work) and the Recipe. The correct result will be Recipe 1 and Recipe 3.

    The easy first step (you’ve done it already) is to get products for the current page and their ID’s into an array:

    // GET PRODUCTS FOR THE CURRENT PAGE (two products)
    $products = get_field('products_under_work');
    // var_dump($products); // returns "Product 1" (id=15) and "Product 3" (id=19)
    
    // GET AN ARRAY OF PRODUCT ID'S ONLY
    $product_ids = array();
    foreach($products as $product) :
    	array_push($product_ids, $product->ID);
    endforeach;
    // var_dump($product_ids); // 15,19
    

    (continued in next post)