How to use custom schema with Rank Math?

#633383
  • Resolved Junuk Seo
    Rank Math free

    Hello, I’m currently using Rank Math’s Product Schema.

    The schema functions as expected, but I’ve encountered an issue with the missing aggregateRating and Review schema that Google Search Console has flagged. To address this, I created the following code:

    add_filter( 'woocommerce_structured_data_product', 'custom_product_structured_data', 10, 2 );
    function custom_product_structured_data( $markup, $product ) {
        if ( empty( $markup['aggregateRating'] ) ) {
            $markup['aggregateRating'] = array(
                "@type"       => "AggregateRating",
                "ratingValue" => "0",
                "reviewCount" => "0",
            );
        }
        
        if ( empty( $markup['review'] ) ) {
            $markup['review'] = array(
                "@type"        => "Review",
                "reviewRating" => array(
                    "@type"       => "Rating",
                    "ratingValue" => "0",
                    ),
                "author"      => array(
                    "@type"  => "Person",
                    "name"   => "",
                   ),
            );
        }
        
        return $markup;
    }

    This code displays both aggregateRating and Review as ‘0’ when no reviews have been written for a product.

    However, this schema seems to stop working when I enable Rank Math. Is there a way to make it work in conjunction with Rank Math?

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.

    If you’re using the product schema from Rank Math, then the filter won’t affect it. Could you please share the affected URL so we can check as well?

    Meanwhile, if you want to remove those in the product schema, you can use and customize this filter:

    add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
    $entity['aggregateRating']['ratingValue'] = '';
    $entity['aggregateRating']['reviewCount'] = '';
    $entity['review']['reviewRating']['ratingValue'] = '';
    $entity['review']['reviewRating']['worstRating'] = '';
    $entity['review']['reviewRating']['bestRating'] = '';
    return $entity;
    });

    Let us know how this goes.

    Thank you.

    hello. Thanks for your answer.

    I applied your code and it solved the “aggregateRating” issue, but I got a new error “Invalid object type for field “review” (optional)”.

    Can you please provide a solution to this issue as well? I’ll leave a link to my website where you can test the schema.

    https://seoartgallery.com/en/product/persimmon-tree-painting/
    Thanks!

    Hello,

    This is because the current reviewRating property is empty. Please make sure you have at least one review for that product page.

    With that said, this warning is simply optional so this shouldn’t cause major issues until you have added a review on your page.

    Hope that helps and please do not hesitate to let us know if you need my assistance with anything else.

    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 ‘How to use custom schema with Rank Math?’ is closed to new replies.