FAQ Schema with meta tags plugin

Im trying to get the meta tags plugin to output FAQ schema. Im having a hard time since its a loop inside an array. I want to drive it from a structure field on each page. Essentially a series of question and answers. It looks like this…

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": [{
    "@type": "Question",
    "name": "What is the return policy?",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "Most unopened items in new condition and returned within <strong>90 days</strong> will receive a refund or exchange. Some items have a modified return policy noted on the receipt or packing slip. Items that are opened or damaged or do not have a receipt may be denied a refund or exchange. Items purchased online or in-store may be returned to any store.<br /><p>Online purchases may be returned via a major parcel carrier. <a href='http://example.com/returns'> Click here </a> to initiate a return.</p>"
    }
  }, {
    "@type": "Question",
    "name": "How long does it take to process a refund?",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "We will reimburse you for returned items in the same way you paid for them. For example, any amounts deducted from a gift card will be credited back to a gift card. For returns by mail, once we receive your return, we will process it within 4–5 business days. It may take up to 7 days after we process the return to reflect in your account, depending on your financial institution's processing time."
    }
  }, {
    "@type": "Question",
    "name": "What is the policy for late/non-delivery of items ordered online?",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "Our local teams work diligently to make sure that your order arrives on time, within our normaldelivery hours of 9AM to 8PM in the recipient's time zone. During  busy holiday periods like Christmas, Valentine's and Mother's Day, we may extend our delivery hours before 9AM and after 8PM to ensure that all gifts are delivered on time. If for any reason your gift does not arrive on time, our dedicated Customer Service agents will do everything they can to help successfully resolve your issue. <br/> <p><a href='https://example.com/orders/'>Click here</a> to complete the form with your order-related question(s).</p>"
    }
  }, {
    "@type": "Question",
    "name": "When will my credit card be charged?",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "We'll attempt to securely charge your credit card at the point of purchase online. If there's a problem, you'll be notified on the spot and prompted to use another card. Once we receive verification of sufficient funds, your payment will be completed and transferred securely to us. Your account will be charged in 24 to 48 hours."
    }
  }, {
    "@type": "Question",
    "name": "Will I be charged sales tax for online orders?",
    "acceptedAnswer": {
      "@type": "Answer",
      "text":"Local and State sales tax will be collected if your recipient's mailing address is in: <ul><li>Arizona</li><li>California</li><li>Colorado</li></ul>"}
    }]
  }
</script>

I can get one to work, but i cant seem to get the array wrapped around the question list in order to loop through them. What am i doing wrong?

'FAQPage' => [
  '@type' => 'FAQPage',

      'mainEntity' => [
      '@type' => 'Question',
      'name' => 'What is SEO?',
      'acceptedAnswer' => [
        '@type' => 'Answer',
        'text' => 'This is the process of optimising a website for Search Engines',
      ],

  ],
],

The result of this is:

    <script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "FAQPage",
        "mainEntity": {
            "@type": "Question",
            "name": "What is SEO?",
            "acceptedAnswer": {
                "@type": "Answer",
                "text": "This is the process of optimising a website for Search Engines"
            }
        }
    }
    </script>

How can i get the loop of questions and answers working?

I’m not familiar with the plugin, but where is your loop?

I can get it working manually without the plugin, but id like to move it into the plugin really since thats where all the other meta stuff is. I like to be tidy :slight_smile:

This works, but is independent of the plugin…

<?php

$faqlist = [];

foreach($page->faq()->toStructure() as $faq) {

  $faqlist[] = [
    '@type' => 'Question',
    'name' => $faq->question()->value(),
    'acceptedAnswer' => [
      '@type' => 'Answer',
      'text' => $faq->answer()->value()
    ]
  ];
}

$schema = [
  "@context" => "http://schema.org",
  "@type" => "FAQPage",
  "mainEntity" => $faqlist
];


?>
<script>
<?= json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT |  JSON_UNESCAPED_UNICODE); ?>
</script> 

Can’t you use a closure for the main entity? Like here for the alternates: https://github.com/pedroborges/kirby-meta-tags#link

Looks like it should work, but it comes back empty…

'FAQPage' => [
  '@type' => 'FAQPage',

      'mainEntity' => function ($page) {

          $faqlist = [];

          foreach($page->faq()->toStructure() as $faq) {

            $faqlist[] = [
              '@type' => 'Question',
              'name' => $faq->question()->value(),
              'acceptedAnswer' => [
                '@type' => 'Answer',
                'text' => $faq->answer()->value()
              ]
            ];
          }

          return $faqlist;

      }
],

Gives me this…

<script type="application/ld+json">
{
    "@context": "http://schema.org",
    "@type": "FAQPage",
    "mainEntity": {}
}
</script>

i think the plugin only checks one level of depth if its a callable. doing faqpage > @type > callable does not seem to work.

having said that it might be possible if you create a page model and define a function there to unroll the data. calling the pagemodel function instead of the callable should work.

I’ve solved it with a snippet for now, but i might give that a shot sometime. Thanks for the tip.

a snippet with return options is pretty much the same than what i suggested – so just keep the snippet. :smiley: