Startseite › Toolset Professional Support › [Gelöst] toolset_get_related_post on page with toolsets form (CRED)
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
Verwandte Dokumentation:
Dieses Thema enthält 15 Antworten, hat 2 Stimmen.
Zuletzt aktualisiert von stuart vor 5 Jahren, 8 Monaten.
Assistiert von: Minesh.
Hi guys
*caveat I am not a php guy, but have bolted this together from a few posts and some experience with Toolsets 🙂
What am I trying to do:
Create a review section that updates each time a new one is submitted giving the average.
I have two relationships:
Companies (parent)
Reviews (child)
I have a (CRED) Toolset Form that is on the "Company" parent page to submit the "reviews".
When the form is submitted I use a function /cred api hook to evaluate all "reviews" for a number then save this to the parent 'company' custom field (for an average reviews rating to be displayed on the parent).
This works when I add the form directly to the "Company" page perfectly as I expect - however when adding to a different page it does not (and I would like to).
websitename.com/add-review?parent_company_id=75 <-- is the link on the "review" button.
Now I know this is not working because its trying to look for the relationship of the 'page' - and its not here... so, I tried to modify it but I am stuck.
--- Here is the code that works on the Company page directly with a cred form ---
/* */
/* Shortcode for average of Child:Reviews wpcf-review - ONLY Works on the Parent Post Type */
/* */
add_shortcode('avg-reviews', 'rating_average_func_reviews');
add_action('cred_submit_complete', 'rating_average_func_reviews',10,2);
function rating_average_func_reviews($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==415)
{
$relationship = 'company-review';//Edit the exact slug of the relationship you want to target
$related_post_id = toolset_get_related_post($post_id, $relationship );//can be repeated to get another related post of other type
$child_posts = toolset_get_related_posts(
get_the_ID(), //Post to query by.
'company-review', //Slug of the relationship to query by
'parent', //Name of the element role to query by.
100, //Maximum number of returned results
0, //Result offset
array(
'meta_key' => 'wpcf-review',
'orderby' => 'meta_value',
'order' => 'ASC',
),//Additional query arguments
'post_id', //Determines return type
'child' // which posts from the relationship should be returned
);
$sum = 0;
$num = 0;
foreach ($child_posts as $child_post) {
$ratings = get_post_meta($child_post, 'wpcf-review', true);
if($ratings) {
$sum += $ratings;
$num++;
}
}
$average = 0;
if($num>0) {
//$average = $sum/$num; // dont use this since $num might be wrong if one rating is 0
$average = round($sum/count($child_posts), 1);
}
//Example of what to do with such data
update_post_meta($related_post_id, 'wpcf-review-total', $average);
return $average;
}
}
--- Here is my attempt at making it work ---
now because its on a page that has the Form and its got a URL parameter from the parent post with its ID - i try to use this.
--> this when I get the URL parameter parent ID: $parent_company = ($_GET['parent_company_id']);
add_shortcode('avg-reviews-new', 'rating_average_func_reviews_new');
add_action('cred_submit_complete', 'rating_average_func_reviews_new',10,2);
function rating_average_func_reviews_new($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==415)
{
$relationship = 'company-review';//Edit the exact slug of the relationship you want to target
$related_post_id = toolset_get_related_post($post_id, $relationship );//can be repeated to get another related post of other type
$parent_company = ($_GET['parent_company_id']);
$child_posts = toolset_get_related_posts(
$parent_company, //Post to query by.
$relationship, //Slug of the relationship to query by
'parent', //Name of the element role to query by.
100, //Maximum number of returned results
0, //Result offset
array(
'meta_key' => 'wpcf-review',
'orderby' => 'meta_value',
'order' => 'ASC',
),//Additional query arguments - this might be unnecessary in my case
'post_id', //Determines return type
'child' // which posts from the relationship should be returned
);
$sum = 0;
$num = 0;
foreach ($child_posts as $child_post) {
$ratings = get_post_meta($child_post, 'wpcf-review', true);
if($ratings) {
$sum += $ratings;
$num++;
}
}
$average = 0;
if($num>0) {
//$average = $sum/$num; // dont use this since $num might be wrong if one rating is 0
$average = round($sum/count($child_posts), 1);
}
//Example of what to do with such data
update_post_meta($parent_company, 'wpcf-review-total', $average);
return $average;
}
}
-----------------------------------------------------------------------------------------------------
It doesn't return anything ... so I am obviously doing something wrong.
Help 🙂 Please
Hello. Thank you for contacting the Toolset support.
Well - I need to check how you passed the URL param to your form so that you can catch the URL param "parent_company_id".
Can you share problem URL where I can see the edit link?
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Well - after checking the functions.php file I found that you are using almost same code for the same form ID 415 multiple times, so two cred_submit_complete actions will be fired every time.
Can you please share where you are using the shortcode avg-reviews-new? on which page?
thanks for your assistance - on this page: versteckter Link
Well - I checked the elementor template assigned to the page:
=> versteckter Link
I am not able to find where exactly the shortcode [avg-reviews-new] is added? Can you please share few screenshot where I can find that.
However, your original issue is to update the parent field "review-total" when we submit the form using the following link - correct?
=> versteckter Link
sorry I removed the shortcode from the page as it wasnt doing anything but highlighting the field was not actually being filled in during the cred submit.
versteckter Link <-- i just created a review
The "Review" fields should have a number.
Feel free to submit a reviews from this page by clicking the "review" button.
versteckter Link
You can see it working on the same page if you fill in the review form at the bottom of the page.
The "Review" fields should have a number.
=> Sorry but where the field "Review" is stored? I do not see that field when I check the review you created?
- versteckter Link
Is "Review" field is stored as post title?
Is the flow adds the start rating review and then fill out the review form?
images added - front end options with the arrows
contained in a content template: [wpv-post-body view_template="star-selection"]
backend - is the field in the CPT
Ok - The reason why "Review" field is not able to save the selected review value because the star reviews are setup outside the Toolset forms so the form do not have access to the star review field.
Maybe you can try some custom JS and add one hidden field to your form so that whenever user select the star review assign the selected value to the hidden field but again this needs custom code which is beyond the scope of our support policy.
I think that actually may have been introduced by me whilst in was waiting for a response (sorry).... ill add the options back to the body of the form and it will be the original problem.
Ok - please let me know once you are done with that so I can check the issue further.
I cant currently edit as you have taken over - see image... could you log out?
OK - sure. Please let me know once you set up the field.
i think its working.... its late here so may not be correct.
but ive moved the options and the tested the form externally and the form on the parent... and the review numbers go up (once published using a view) and the stars increase... so not sure what was the issue.
one things I cant seem to resolve is moving the stars on to their own line... but thats a css issue
Yes - thats a different issue. Please open a new ticket with your every question you many have. This will help other users searching on the forum. Thank you for understanding.