I am working on this portal in which users can review the services (with description and rating) of a cultural place.

BASICALLY, I created two Custom Post Types (CPT) with Pods, one is cultural_places (CP) and the other is reviews (RW).

THEN in the archive pages, I sort the CPs by their services RWs average ratings.

BUT for this, I have two loops: RWs inside CPs. Every RWs loop confronts the related ID field with CP's, sum all RWs ratings in a variable, before the end of CPs loop I store the obtained average rating in an array with CP ID and RWs count. Then outside of the loops, I create a for loop where I sort the array by the column of the average rating. Finally, I do another for loop with the sorted array where I print the CPs with their services average rating.

Example, best shown in Update 1 of Sort custom post type by other custom post type field. (Places by their reviews mean vote):

CP while{
        RW while{
                If(RW related field == CP_ID){
                                             Sum ratings
                                             Count RWs
        Calculate average_rating = RWs ratings sum / RWs count
        Store CP_ID in new_array[0]
        Store average_rating in new_array[1]
        Store RW_count new_array[2]
usort array by average_rating column new_array[1]
   print sorted new_array

NOW, everything works fine and while I have little data (CPs and RWs) it doesn't take long but I can already notice a little delay in the page load. For obvious reasons.

SO, firstly, I wanted to know if there's a way to store that average rating directly as CPT meta or as a column in WP post table. And if not, how to store all this data in a custom table. Maybe even creating Advanced CPT that directly store it's data to a custom table? But still, the average rating calculations would be left off. Or there's also a way to make those calculations DB automatised? Maybe using a template part?

The point, of course, is to lighten the page workload.

Thanks a lot for anyone who can help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.