Custom field to Schema

#889314
  • Resolved Miko Hody
    Rank Math free

    Hey,

    I need some help. I have a custom plugin that helps me get ranking data from Google using API to my own database. I display this data (rating rating and number of ratings) on my site using a shortcode. I am looking for a solution to insert this data into Schema using RankMath. I have my own schema where I have already defined several items including e.g. custom fields using ACF.

    Can you tell me how I can register custom variables for my dynamic fields? Can I use a shortcode in the schema? Please note that I am not looking for a solution for ACF! ๐Ÿ™‚ I’ve gone through your support, but I haven’t found anything to help me.

    I tried hard to add data to the schema, but in my schema nothing is displayed about the rating… ๐Ÿ™

    
    
    add_filter('rank_math/json_ld', 'add_custom_rating_to_schema', 10, 2);
    function add_custom_rating_to_schema($data, $post) {
        if ('turistika' === get_post_type($post)) {
            $place_data = gpr_get_place_data_from_db($post->ID);
    
            if ($place_data && isset($place_data->rating) && isset($place_data->rating_count)) {
                $data['aggregateRating'] = array(
                    '@type' => 'AggregateRating',
                    'ratingValue' => sprintf('%.1f', $place_data->rating), 
                    'ratingCount' => intval($place_data->rating_count)
                );
            }
        }
    
        return $data;
    }
    
    
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello,

    We apologize for the issue you are facing with integrating your custom rating data into the Schema using Rank Math. Thank you for providing the details about your setup.

    From your code snippet, it looks like you are on the right track by using the rank_math/json_ld filter to add your custom rating data. However, there are a few things to consider:

    1. Ensure Post Type Check: Make sure that the post type check (turistika) is correct and that you are indeed working with the right post type when this function is called.

    2. Check Data Retrieval: Verify that the function gpr_get_place_data_from_db($post->ID) is returning the expected data. You can add some debugging statements (like error_log()) to check if $place_data contains the correct values.

    3. Schema Structure: The structure you are using for aggregateRating seems correct. Just ensure that the values being passed to ratingValue and ratingCount are valid and formatted correctly.

    4. Shortcodes in Schema: Unfortunately, you cannot directly use shortcodes within the Schema markup. Instead, you should retrieve the data directly from your database or wherever it is stored, as you are doing in your code.

    5. Testing: After implementing your changes, use the Google Rich Results Test to validate your Schema output. This tool will help you see if your Schema is being generated correctly and if there are any errors.

    If after checking these points you still face issues, please reply to this ticket, and a human agent from the Rank Math Support team will assist you further.

    We hope this helps you resolve the issue!

    Sources:
    How to Use ACF Fields in Schema Generator ยป Rank Math

    Hello,

    Thank you for contacting support.

    The aggregateRating property needs to be associated with one of the supported types mentioned in Google documentation here: https://developers.google.com/search/docs/appearance/structured-data/review-snippet

    In your code, you are just adding this to the $data array without associating this with any of the types present on that array or by creating a new custom entry point inside that array.

    To get a better understanding of the data that is inside that array you can dump its values and see if any of the generated data you currently have would be ideal to be associated with the rating data according to the guidelines we mentioned above.

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

    Although I modified the code, it did not appear in the schema. Let’s leave it alone. Is there a way to add a custom dynamic field with a transformation to the schema from my plugin?

    Hello,

    Please confirm the Schema type to which you want to add aggregateRating property and how your plugin saves the rating data in the database so we can assist you further.

    Looking forward to helping you.

    Hey,

    thanks for your help. I’ve attached below the data in our database that we need to work with and also our schema.

    
    {
      "@context": "https://schema.org",
      "@graph": {
        "@type": "TouristAttraction",
        "name": "%title%",
        "description": "%excerpt%",
        "email": "%customfield(ku_email)%",
        "telephone": "%customfield(ku_telefon)%",
        "image": "%post_thumbnail%",
        "sameAs": "%customfield(ku_web)%",
        "publicAccess": "true",
        "isAccessibleForFree": "%customfield(vstupne_zdarma)%",
        "geo": {
          "@type": "GeoCoordinates",
          "longitude": "%customfield(ku_longitude)%",
          "latitude": "%customfield(ku_latitude)%"
        },
        "address": {
          "@type": "PostalAddress",
          "addressCountry": "CZ",
          "addressLocality": "%customterm(mesto)%"
        }
      }
    }
    
    

    DB

    Hello,

    The TouristAttraction schema property is not applicable to have an aggregateRating property. You must add it to any of the following schema types:

    -Book
    -Course list
    -Event
    -Local business
    -Movie
    -Product
    -Recipe
    -Software App

    As for the database table shown from your screenshot, the data structure here is not formatted for custom fields as this is a custom table. You’ll have to query them manually. Here’s an example implementation:

    function get_google_places_rating_by_post_id($post_id) {
    	global $wpdb;
    
    	// $table_name = $wpdb->prefix . 'google_places_rating';
    	$table_name = 'imd7_google_places_rating';
    
    	$query = $wpdb->prepare(
    		"SELECT rating, user_ratings_total, time_updated 
    		FROM $table_name
    		WHERE post_id = %d", 
    		$post_id
    	);
    
    	$rating_data = $wpdb->get_row($query);
    	return $rating_data ? $rating_data : null;
    }
    
    add_filter('rank_math/snippet/rich_snippet_product_entity', function ($entity) {
    	global $post;
    	$rating = get_google_places_rating_by_post_id($post->ID)->rating;
    
    	$entity['aggregateRating'] = [
    		'@type'       => 'AggregateRating',
    		'ratingValue' => $rating,
    		'bestRating'  => '5',
    		'worstRating' => '1',
    		'ratingCount' => '72',
    	];
    	return $entity;
    });

    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 6 replies - 1 through 6 (of 6 total)

The ticket ‘Custom field to Schema’ is closed to new replies.