Last active
August 31, 2023 01:18
-
-
Save khoipro/c0595ea175e32653ab4ddfd907b524f0 to your computer and use it in GitHub Desktop.
Fix Rank Math product schema (single product)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Fix missing review on product schema (Rank Math) | |
* | |
* @author codetot | |
**/ | |
function codetot_update_missing_product_schema( $entity ) { | |
$stampedDBData = get_post_meta(get_queried_object_id(), 'stamped_io_product_reviews_new', true); | |
if (isset($stampedDBData) && $stampedDBData['count'] > 0 ) { | |
$entity['aggregateRating']['@type'] = "AggregateRating"; | |
$entity['aggregateRating']['ratingValue'] = $stampedDBData['rating']; | |
$entity['aggregateRating']['ratingCount'] = $stampedDBData['count']; | |
} else { | |
$entity['aggregateRating'] = [ | |
'@type' => 'AggregateRating', | |
'ratingValue' => '5', | |
'ratingCount' => 0 | |
]; | |
} | |
return $entity; | |
} | |
add_filter( 'rank_math/snippet/rich_snippet_product_entity', 'codetot_update_missing_product_schema'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hay quá a