Aggregate rating code snippet placed in function.php file. No luck

#466386
  • Resolved Gerrie Bezuijen
    Rank Math pro

    Hi,

    I’ve placed the code snippet in my function.php file. What must I exactly change regarding replace with your rating post meta. Is this a snippet valid for one single product? I don’t understand what to enter here.

    https://rankmath.com/kb/aggregate-rating-error/

    Code snippet:

    add_filter(‘rank_math/snippet/rich_snippet_product_entity’, function ($entity) {
    $score = get_post_meta(get_the_ID(), ‘users_rating’, true); // Replace with your rating post meta.
    $count = get_post_meta(get_the_ID(), ‘users_rating_count’, true); // Replace with your rating count post meta.
    $entity[‘aggregateRating’] = array(
    ‘@type’ => ‘aggregateRating’,
    ‘ratingValue’ => $score,
    ‘ratingCount’ => $count,
    );
    return $entity;
    });

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.

    Rank Math does not have a feature to accept reviews from users/visitors. If you want to collect the user ratings, then you will have to use a third-party plugin like WP Review to do that.

    Here’s a link for an example where we use the WP Review plugin as an example to get the ratingValue and ratingCount variables:
    https://rankmath.com/kb/schema-templates/#aggregateRating

    Hope that helps.

    Thank you.

    my bad I must’ve been unclear.

    I know Rank Math does not provide this and I already use a review plugin. My question is meant totally different.

    Google Search console noticated me that for a particular product the aggregate rating schema is not found; while the product has 1 review.

    Please see the rank math article below.

    https://rankmath.com/kb/aggregate-rating-error/

    In this article Rank Math explaines how to solve this by placing a code snippet in the functions.php file. In this code snippet I have to replace the users_rating with my own post meta. And I have to replace the users_rating_count with the products rating count.

    Code snippet:

    add_filter(‘rank_math/snippet/rich_snippet_product_entity’, function ($entity) {
    $score = get_post_meta(get_the_ID(), ‘users_rating’, true); // Replace with your rating post meta.
    $count = get_post_meta(get_the_ID(), ‘users_rating_count’, true); // Replace with your rating count post meta.
    $entity[‘aggregateRating’] = array(
    ‘@type’ => ‘aggregateRating’,
    ‘ratingValue’ => $score,
    ‘ratingCount’ => $count,
    );
    return $entity;
    });

    What I want to know is that…with what information do I have to replace this? where do I find this information.

    Hello,

    The values for that would be inside the postmeta table with the ID of the post or page where you are trying to apply this method.

    If you are using the plugin mentioned in the tutorial you can use the keys wp_review_user_reviews and wp_review_review_count.

    Don’t hesitate to get in touch if you have any other questions.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Aggregate rating code snippet placed in function.php file. No luck’ is closed to new replies.