Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9324125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:31:10+00:00 2026-06-19T04:31:10+00:00

I have a custom post type (product) , which includes a custom number field

  • 0

I have a custom post type (product) , which includes a custom number field (rating). On a category (manufacturer) page I want to average those ratings to give an aggregate rating for the manufacturer.

So I am trying to build a function that returns a single value:

  • retrieves the average value of all non-empty ratings fields found within the custom posts that are categorized with this category
  • runs independently of the loop (I need to display before the loop and sometimes have more posts than are being used by the loop)

Any ideas how to tackle this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T04:31:11+00:00Added an answer on June 19, 2026 at 4:31 am

    You create custom template which you would apply to your Manufacturers page. Outwith the main loop you would create a secondary loop using WP_Query. You then run through your custom post type and cumulate the ratings where they exist. Set up a counter to calculate the average. Then use wp_reset_query() to reset the loop if necessary.

    I’ve not tested the code below (obviously, I’m not going to go and set all that up), but it should give you enough of an idea that you can tweak/debug it to get the desired result;

    I have created a fictitious category ID within you custom post type of 4 in this example, you can change it to whatever your setup is;

    <?php
    $counter = 0;
    $cumulative_value = 0;
    
    $avg_query = new WP_Query(array('post_type' => 'product', 'posts_per_page' => '-1', 'cat' => '4' ));
    
    if ( $avg_query->have_posts() ) :  
        while ($avg_query->have_posts()) : $avg_query->the_post(); 
    
            if ( $rating_value = get_post_meta($post->ID, 'rating', true) ) :
    
                $cumulative_value += $rating_value;
                $counter++;
    
            endif;
    
        endwhile;
    
    endif;
    
    $avg_value = $cumulative_value/$counter;
    
    wp_reset_query(); ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom post type FAQ and custom taxonomy Category. I want to
I have a custom post type (product) that have a meta field that hold's
I have created a custom post type named People. I have created a page
i have created custom post type 'event' to display in single page named single-event.php
I have created a custom post type of 'portfolio' and page with a template
I have a custom post type which displays all my entires upside down, meaning
I have created a custom post type called banners and want to enable the
I have created a custom post type calles articles and a custom category like
I have to show all the posts from custom type post on my product
I have a WooCommerce site which has a custom post type named 'products'. Each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.