Missing field mainEntity in Yoast Profile Page Schema

Missing field mainEntity in Yoast Profile Page Schema

We encountered an error Missing field ‘mainEntity’ when applying the Profile Page schema on a site’s bio pages using Yoast SEO WordPress plugin.

We found the error when validating the schema on bio pages using Google’s Rich Results Test and also through Google Search Console.

When Google crawled the site, we saw an error preventing Google from indexing the schema because the mainEntity parameter was missing within the schema.

Google Search Console Profile Schema Results
Google Search Console Profile Schema Results

In Google Search Console the error is:

Missing field ‘mainEntity’

Items with this issue are invalid. Invalid items are not eligible for Google Search’s rich results

Our cracking developer, Anam, dug around the Yoast API documentation and wrote the following code to inject the mainEntity parameter into the Profile schema.

/**
 * Update Yoast Profile Page schema markup to include mainEntity
 */
add_filter( 'wpseo_schema_webpage', 'update_bio_schema_markup_of_yoast' );
/**
 * Changes @type of Webpage Schema data.
 *
 * @param array $data Schema.org Webpage data array.
 *
 * @return array Schema.org Webpage data array.
 */
function update_bio_schema_markup_of_yoast( $data ) {
	if ( is_singular('lnwm_bio') ) {
		$bio_title = get_the_title(get_the_ID());
		$teamtitle = get_field( 'ln_team_title' );
		$teamtitle = $teamtitle ? $teamtitle : '';
		$bio = get_field( 'ln_team_bio' );
		$bio = $bio ? $bio : '';
        $data['mainEntity'] = (object)[
			'type' => 'person',
			'ID' => get_the_ID(),
			'name' => $bio_title,
			'title' => $teamtitle,
			'content' => $bio
		];
    }
    return $data;
}

Once we added this code to the site and requested that Google review the fix, all URLs now show as having valid Profile schema.