Accessing Schema Data In the Loop

#51868
  • Resolved Dan Jones
    Rank Math free

    Hello,

    I have a bunch of posts that use the product schema. This includes a star rating.

    I would like to access the star rating data and show it in my post listing (using the loop).

    I considered the following options:

    1. Use a built-in RankMath hook – Does this exist? I couldn’t find one
    2. Access the database table directly – I couldn’t find any table with the product rating data

    What’s the best way to do this?

    Thanks in advance!

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

    Thank you for contacting Rank Math today.

    Do you have a custom product CPT or your are using WooCommerce?

    If it is the latter, the data is saved by WooCommerce and you can grab the rating data using the following logic:

    $product = wc_get_product( $product_id );
    $rating  = $product->get_average_rating();

    If you have a CPT then you can grab the value from the postmeta table by using:
    $value = get_post_meta($post->ID,'rank_math_snippet_product_rating', true);

    Looking forward to helping you. Thank you.

    ​​​​​​

    Thank you, that worked perfectly!

    For anybody that has this problem and uses vanilla WordPress (i.e. not WooCommerce), this is how you can get the current rating and the maximum possible rating:

    get_post_meta(get_the_ID(),'rank_math_snippet_product_rating', true);

    get_post_meta(get_the_ID(),'rank_math_snippet_product_rating_max', true)

    And if this only applies to some posts, you can use this if statement:

    metadata_exists('post', get_the_ID(), 'rank_math_snippet_product_rating')

    • This reply was modified 3 years, 11 months ago by Dan Jones.
    • This reply was modified 3 years, 11 months ago by Dan Jones.
    • This reply was modified 3 years, 11 months ago by Dan Jones.
    Todd
    Rank Math free

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

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

The ticket ‘Accessing Schema Data In the Loop’ is closed to new replies.