Hello,
Thank you for contacting Rank Math and bringing your concern to our attention.
1. Unfortunately, sorting and rearranging is not possible for Rank Math’s FAQ block right now but this is indeed a good feature to have.
I have passed this on to our developers and hopefully, this will be added to our future development roadmap.
2. HTML can be used in the Schema blocks. You can use the code below:
add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
if( ! have_rows('acf-repeater-faq') ) {
return $data;
}
$data['faqs'] = [
'@type' => 'FAQPage',
];
while( have_rows('acf-repeater-faq') ) {
the_row();
$data['faqs']['mainEntity'][] = [
'@type' => 'Question',
'name' => esc_attr( get_sub_field('faq_question') ),
'acceptedAnswer' => [
'@type' => 'Answer',
'text' => esc_attr( get_sub_field('faq_answer') ),
],
];
}
return $data;
}, 10, 2 );
Please note that you will need to create ACF Repeater fields, then change these fields in the above code
acf-repeater-faq
faq_question
faq_answer
I hope that helps. Thank you, and please don’t hesitate to contact us if you need further assistance.
Hey Reinelle,
I have the same issue.
I replicated your suggestion, but it seems that the filter doesn’t catch the ACF Block (repeater).
The Front-End (visual) is working as expected. But doesn’t show the Schema Markup for FAQPage.
When I toggle
"if( ! have_rows('acf-repeater-faq') "
to
if( have_rows('acf-repeater-faq')
It shows a bit of information (until the while loop). This indicated the filter doesn’t find the ACF Block.
I think the code needs to loop over the content to find and pull the data. Could you help with that?
Got it resolved!
add_filter('rank_math/json_ld', function ($data, $jsonld) {
$pid = get_post();
// check if acf is used
if (function_exists('get_field')) {
if (has_blocks($pid)) {
$blocks = parse_blocks($pid->post_content);
// run trough acf gutenberg/acf-blocks and find the right one
foreach ($blocks as $block) {
// find all selected FAQs
if ($block['blockName'] == 'acf/faq') {
// Start building the FAQPage Schema
$data['faqs'] = [
'@type' => 'FAQPage',
];
// build new array from found FAQs
$block_data = $block['attrs']['data'];
// get total number of Q&A
$total_faq = $block_data['acf-repeater-faq'] - 1;
// loop X times according the number of Q&A
for ($x = 0; $x <= $total_faq; $x++) {
// start bulding the Q&A Schema for each repeater
$data['faqs']['mainEntity'][] = [
'@type' => 'Question',
'name' => esc_attr($block_data['acf-repeater-faq_' . $x . '_faq_question']),
'acceptedAnswer' => [
'@type' => 'Answer',
'text' => esc_attr($block_data['acf-repeater-faq_' . $x . '_faq_answer']),
],
];
}
return $data;
} else {
continue;
}
}
}
}
}, 10, 2);
Hello @Jose
We are glad that your issue has been resolved and thanks for sharing those codes.
Please feel free to reach out to us again in case you need any other assistance. We are here to help.
Thank you.
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.