Hi,
I can not get my head around how to set up a WP_Query to make use of data that I have stored in fields of intermediary posts.
I am creating a website with two custom post types, Recipes and Ingredients, and a many-to-many relationship between them (A recipe has many ingredients and an ingredient can be used in many recipes). What makes each relation unique is the amount of the ingredients, so I store Amount as a field in the intermediary post. The Ingredient post stores the basic nutritional information for ingredients (kcal, fat, carbs, protein per 100g, for example).
When calculating the total kcal, fat, carbs and protein content of the recipe I need to make use of the Amount data when querying by ingredients (to sum up the total nutritional values for the recipe).
I’m new to PHP programming but I have made all of this work when I use the Ingredient post to store the Amount data, but then obviously I can’t use M2M relationship anymore.
I have read everything I could find about the Toolset_relationships in a WP_Query but simply can’t figure out how to set this up.
Best regards
Sigfus
Hi Sigfús,
As discussed in the chat, this is the new support ticket, so that you can share the temporary admin access details, when ready.
Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.
regards,
Waqar
Thank you for sharing these details and the login access works.
I'm going to review your website's setup and will test the code on my test website accordingly.
Thank you for your patience.
Hi Waqar. Looking forward to your response. In the nutshell, my (general) issue is as follows: How can I access the field in the relationship (intermediary) that is uniqe for each parent-child relation, when looping through the child posts?
Thank you for waiting.
Reviewing the setup and the requirements, you'll need the ID of the related ingredients post as well as the respective intermediary post, for your calculations.
Both these can be retrieved using the "toolset_get_related_posts" function:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
For example, here is the restructured shortcode:
add_shortcode( 'sbj_recipe_ingred_calculations', 'sbj_recipe_ingred_calculations_func');
function sbj_recipe_ingred_calculations_func($atts)
{
// set defaults
$a = shortcode_atts( array(
'recipe' => get_the_ID(),
), $atts );
// set the target recipe
$target_recipe_ID = $a['recipe'];
// set relationship slug
$relationship_slug = 'recipe-ingredient';
// get all related posts of the target recipe
$query_by_element = $target_recipe_ID; // ID of post to get relationship from
$relationship = $relationship_slug; // relationship slug
$query_by_role_name = 'parent'; // $query_by_element is a parent in this relation
$limit = 1000; // defaults
$offset = 0; // defaults
$args = array(); //nothing needed
$return = 'post_id'; // We want Post ID
$role_name_to_return = 'all'; // We want all items in the relationship.
$get_results = toolset_get_related_posts(
$query_by_element,
$relationship,
$query_by_role_name,
$limit,
$offset,
$args,
$return,
$role_name_to_return
);
// if some related posts are found
if(!empty($get_results)) {
foreach ($get_results as $get_result) {
// the ID of the related ingredient
$target_ingredient_ID = $get_result['child'];
// the ID of the related intermediary
$target_intermediary_ID = $get_result['intermediary'];
}
}
}
Please replace "recipe-ingredient" with the actual relationship slug used on your website. Since you'll have the IDs of both, the ingredient and the intermediary posts in the foreach loop, you can call custom fields values from both too.
I hope this helps.
Dear Waqar
Thank you for the professoional help and the code. Being a newcomer to PHP it took me some time to understand it and to "marry" your new part of the code to my old part (retrieval of values and calculations) but I learned a lot along the way and to there in the end:) Everything works fine now! What a great plugins Toolset offers, with a great supprt.
My issue is resolved now. Thank you!
Sigfús