FAQPage schema in PHP not formatted correctly

Viewed 40

I'm trying to build a schema for a page that has FAQ questions. The code works but something is wrong with the formatting. The problem is that the array "mainEntity" is on every question.

Here is my code


    $posts = get_field('section_faq_relation');
    if( $posts ): 
        
        $schema_faq_questions = array();
        
        foreach( $posts as $post):
            setup_postdata($post);
            $headline = get_the_title();
            $articlebody = get_the_content();
            $schema_faq_question = array(
                '@type'     => 'Question',
                'name'     => $headline,
                'acceptedAnswer' => array(
                    '@type'     => 'Answer',
                    'text'     => wp_filter_nohtml_kses( $articlebody )
                 )
            );
        
         array_push($schema_faq_questions, $schema_faq_question);
         endforeach;
        
         $schema_faq_all = array(
             '@context'  => 'http://schema.org',
             '@type'     => 'FAQPage',
             'mainEntity' => $schema_faq_questions
         );
    
        echo '<script type="application/ld+json">' . json_encode($schema_faq_all) . '</script>';
    endif; 
    wp_reset_postdata();

My code looks OK and returns this if I look at the json it's outputting. mainEntity is an array that holds all the questions.

{
  "@context": "http://schema.org",
  "@type": "FAQPage",
  "mainEntity": [

      {
        "@type": "Question",
        "name": "dolor sit amet",
        "acceptedAnswer": {
          "@type": "Answer",
          "text": "Lorem ipsum dolor sit amet, consectetur adipiscing eli"
        }
      },
      {
        "@type": "Question",
        "name": "quasi architecto",
        "acceptedAnswer": {
          "@type": "Answer",
          "text": "re veritatis et quasi architecto beatae vitae dicta sunt explicabo."
        }
      }
    ]

}

But if I run the schema.org validator it returns:

schema output FAQPage

Every question has "mainEntity". I don't know why it's doing that.

0 Answers
Related