Support

Account

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

Solving

Query multiple values in relationship field

  • Hello hello,

    I couldn’t manage to find out how to do this.

    So I have a page “Work”, and 2 custom post types “Recipies” and “Products”.

    The page “Work” has a custom field relationship with several “products”.
    Each “Recipes” have a custom field relationship with several “products”.

    What I want is to display the page “Work” and get all the recipies which belongs that have a relationship with all the products of this page.

    I hope it makes sense !

    Here is what I started, and it doesn’t work. I know I should use the “LIKE” compare but that wouldn’t allow me to get multiple values then …

    // Empty array to get the products IDs
    $produits_ids = array();
    
    // We get all the products all the page "Work"
    $produits = get_field('liste_produits', $metier_id);
    
    foreach($produits as $produit) :
    
    	array_push($produits_ids, $produit->ID);
    
    endforeach;
    
    $args = array(
    	'post_type'		=> 'recette',
    	'meta_query'		=> array(
    		array(
    			'key'			=> 'liste_produits_recette',
    			'value' 		=> $produits_ids,
    			'compare' 		=> 'IN'
    		)
    	)
    ); 
    
    $query = new WP_Query($args);

    I hope you will be able to help me with this 🙂

    Thanks

  • 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)

  • 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.

  • 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);

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Query multiple values in relationship field’ is closed to new replies.