0

in multi author website when author publish post , he/her can give star rating to his/her own post. and I can display average rating for every taxonomy term using this formula:

AVERAGE RATING=TOTAL STAR RATING SUM / TOTAL POST COUNT IN CURRENT TAXONOMY TERM ARCHIVE.

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<span  id=stars></span>
<?php
$term_slug = get_queried_object()->slug;
$args = array(
  'posts_per_page'   => -1,    // Number of posts per page
  'post_type'     => array('A-CPT','B-CPT'),  // Custom Post Types
  'tax_query'     => array(
              array(
                'taxonomy' => 'MY-TAXONOMY-SLUG-NAME',  //Custom Taxonomy Name 
                'field' => 'slug',
                'terms' => array( 
                   $term_slug
                )
              )
            )
  );
  
$new = new WP_Query( $args );
 $count = $new->post_count;  // Total post count
if ( have_posts() ) while ($new->have_posts()) : $new->the_post(); ?>
  <?php
$post_id = get_the_ID();
  $post_rating = get_post_meta( $post_id, 'ratings_3421372395', true ); // getting rating metavalues from posts
?>
<?php
if (!empty($post_rating)){
        $reviewsNum += $post_rating;
    }
?>
<?php endwhile; // end of the loop. ?>
<?php
$averagerating = $reviewsNum / $count; ?> 
<?php  //echo $count; ?>
<?php // echo $reviewsNum; ?>


<script>
    document.getElementById("stars").innerHTML = getStars(<?php echo  $averagerating; 
?>);

function getStars(rating) {

  // Round to nearest half
  rating = Math.round(rating * 2) / 2;
  let output = [];

  // Append all the filled whole stars
  for (var i = rating; i >= 1; i--)
    output.push('<i class="fa fa-star" aria-hidden="true" style="font-size: 20px; color: green;"></i> ');

  // If there is a half a star, append it
  if (i == .5) output.push('<i class="fa fa-star-half-o" aria-hidden="true" style="font-size: 20px; color: green;"></i> ');

  // Fill the empty stars
  for (let i = (5 - rating); i >= 1; i--)
    output.push('<i class="fa fa-star-o" aria-hidden="true" style="font-size: 20px; color: green;"></i> ');

  return output.join('');

}
</script>

But I also need to save average rating result into db for current taxonomy term. thatswhy i created custom meta field for taxonomy terms ( Example: avareage_star_rating) . But how can I save average rating in db automaticly

5
  • Your code appears to be incomplete, there is no code in your question to send the new rating back to the server that can be used to calculate and update the average. Doing it in javascript means anybody could change the average and abuse the system. This has to be done server side in PHP.
    – Tom J Nowell
    Mar 20, 2021 at 18:55
  • average rating calculated with php. javascript is used to display output value as stars Mar 20, 2021 at 18:58
  • I'm not sure what the question is then, can you describe using different words? Are you trying to avoid calculating the average by saving it? Can't you do that when a post is saved?
    – Tom J Nowell
    Mar 20, 2021 at 19:46
  • Example: I display total post count in taxonomy term archive. Then how can I save this result in database of this taxonomy term custom meta field Mar 20, 2021 at 19:53
  • So it's specifically, how to save term meta? If so, is update_term_meta the answer?
    – Tom J Nowell
    Mar 20, 2021 at 20:25

1 Answer 1

0

You can save term meta using update_term_meta:

update_term_meta( int $term_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' )

https://developer.wordpress.org/reference/functions/update_term_meta/

3
  • thanks I will try now and i also found something useful about $wpdb->update Mar 20, 2021 at 20:29
  • ok. I think update_term_meta is better. Thank you very much. Tom J Nowell Mar 20, 2021 at 20:33
  • 1
    it worked like charm thank you very much again. I had absolutely no idea how to fix this problem. Thanks sooo much. You are best Mar 20, 2021 at 21:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.