Skip Navigation

[Resolved] Post relationships API help

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by geophray 4 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1549079

Tell us what you are trying to do?
I'm trying to write a function for calculating the value of a field when the post is updated based on the value of a different field in all related posts of a different type. Here is what I have so far with notes on what I'm trying to accomplish in each line.

// When a Staff Member bio is created or updated, trigger the updateStaffReviewStats() function.
add_action('save_post_{$post->staff}', 'updateStaffReviewStats', 10, 1);
// Function for updating the aggregate review rating and review count for a staff member
function updateStaffReviewStats($post_ID) {
    // Find out if there are any reviews related to the Staff Member
    $myReviews = toolset_get_related_posts(
        $post_ID,
        'staff-member-review',
        [
            'query_by_role'     => 'parent',
            'role_to_return'    => 'other',
            'args'              => [ 'post_status' => 'published' ]
        ]
    );
    // If there are no related reviews, exit function
    if(!$myReviews) { return; }
    // Otherwise set reviewSum and reviewCount to 0
    $reviewSum = 0;
    $reviewCount = 0;
    // Loop through related reviews
    foreach($myReviews as $review) {
        // Calculate the average rating value 
        $reviewSum = $reviewSum + $review['wpcf-review-rating'];
        // Count total number of reviews 
        $reviewCount++;
    }
    // Calculate the average and store it in $avgRating
    $avgRating = $reviewSum / $reviewCount;          
    // store avgRating in the wpcf-aggregate-staff-member-rating field for the current staff member
    // store the count in the wpcf-total-reviews-for-staff-member field for the current staff member
    // Update post 37
    $staff_post = array(
        'ID'           => $post_ID, 
        'wpcf-aggregate-staff-member-rating'   => $avgRating,
        'wpcf-total-reviews-for-staff-member' => $reviewCount
    );

    // Update the post into the database
    wp_update_post( $staff_post );
}

When I update a test post of type "staff" that has multiple reviews associated with it, nothing seems to happen, and I'm not 100% sure where to go from here with regard to troubleshooting.

Is there any documentation that you are following?
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Any guidance you can give with regard to using the toolset api would be greatly appreciated. I know this is beyond the scope of your normal support, so if you can't help me I completely understand. Even a nudge in the right direction would be appreciated.

Thanks a million. 🙂

#1549415

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi there

A couple of observations:

You can't use save_post_{$post->staff} for your action, you need to use the generic save_post action with a priority of at least 30 (https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/).

You seem to be treating custom fields (stored in wp_postmeta) as if they were standard fields that are part of the post object (those stored in wp_posts).

toolset_get_related_posts will return an array of post IDs by default (although you can specify it should return an array of post objects), see https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts. But even an array of post objects has standard fields, not custom fields, and you need to use get_post_meta to retrieve the custom field values (https://developer.wordpress.org/reference/functions/get_post_meta/).

Likewise, wp_update_post will set your standard field values, but to update custom field values you would use update_post_meta (https://developer.wordpress.org/reference/functions/update_post_meta/).

#1557771

Thank you Nigel... this is all VERY helpful. I appreciate the gentle nudge in the right direction. I haven't been able to jump back into this quite yet but I wanted to thank you for your time before my ticket was auto-closed. 🙂

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.