FAQ Automated ACF filter work with ACF Blocks

#1001569
  • Resolved Max
    Rank Math free

    How can I get the automated FAQ Schema code to work with my ACF blocks?

    add_filter( ‘rank_math/json_ld’, function( $data, $jsonld ) {

    if ( ! have_rows( ‘accordion’ ) ) {
    return $data;
    }

    $faq_data = [];

    while ( have_rows( ‘accordion’ ) ) {
    the_row();
    $faq_data[] = [
    ‘@type’ => ‘Question’,
    ‘name’ => esc_attr( get_sub_field( ‘accordion_title’ ) ),
    ‘acceptedAnswer’ => [
    ‘@type’ => ‘Answer’,
    ‘text’ => esc_attr( get_sub_field( ‘accordion_panel’ ) ),
    ],
    ];
    }

    if ( ! empty( $faq_data ) ) {
    $data[‘faqPage’] = [
    ‘@type’ => ‘FAQPage’,
    ‘mainEntity’ => $faq_data,
    ];
    }

    return $data;
    }, 10, 2 );

    I can get some of this to show up and set the page to faq type but I am unable to get the mainEntity to update with all the FAQs/answers.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    I apologize for the issue you are facing with getting the automated FAQ Schema code to work with your ACF blocks. It seems like you’re on the right track with your code, but there might be a couple of things to check to ensure that the mainEntity is populated correctly.

    Here are a few suggestions:

    1. Check Field Names: Ensure that the field names in your ACF setup match exactly with what you are using in your code. For example, confirm that accordion_title and accordion_panel are the correct sub-field names.

    2. Debugging: You can add some debugging statements to check if the have_rows('accordion') condition is returning true and if the loop is executing as expected. You can use error_log() to log messages to your debug log.

    3. Schema Structure: Make sure that the structure of your schema is correct. The mainEntity should be an array of questions, which it seems you are doing correctly. Just ensure that $faq_data is being populated as expected.

    4. Testing: After making changes, test your schema using the Google Rich Results Test to see if there are any errors or warnings that could give you more insight into what might be going wrong.

    If you continue to have issues, please reply to this ticket with more details about your ACF setup or any error messages you might be encountering, and a human agent from the Rank Math Support team will assist you further.

    Thank you for raising this concern!

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

    Max
    Rank Math free

    This works fine with a regular ACF flexible rows layout, but not with ACF Blocks. I am needing it to work with my blocks with names of: acf/faq.

    Hello,

    Thank you for your patience.

    In this case, you can try this filter to fetch the data from your ACF blocks:

    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);

    Please update the values/fields in the code according to your setup and check if it works on your site.

    Let us know how this goes.

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

The ticket ‘FAQ Automated ACF filter work with ACF Blocks’ is closed to new replies.