# FAQ Section With Schema From an ACF Repeater

> Adds an [acf_faq] shortcode that shows a question-and-answer repeater as accessible collapsible items and outputs matching FAQPage structured data.

- Type: PHP
- Runs: Run everywhere
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: acf, faq, schema
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/acf-repeater-faq-schema/

## Code

```php
add_shortcode( 'acf_faq', function ( $atts ) {
    $atts    = shortcode_atts( array( 'name' => 'faqs', 'question' => 'question', 'answer' => 'answer' ), $atts, 'acf_faq' );
    $post_id = get_the_ID();

    if ( ! $post_id || ! function_exists( 'get_field' ) ) {
        return '';
    }

    $rows = get_field( sanitize_key( $atts['name'] ), $post_id );

    if ( ! is_array( $rows ) || ! $rows ) {
        return '';
    }

    $html   = '<div class="acf-faq">';
    $schema = array( '@context' => 'https://schema.org', '@type' => 'FAQPage', 'mainEntity' => array() );

    foreach ( $rows as $row ) {
        $q = isset( $row[ $atts['question'] ] ) && is_string( $row[ $atts['question'] ] ) ? trim( $row[ $atts['question'] ] ) : '';
        $a = isset( $row[ $atts['answer'] ] ) && is_string( $row[ $atts['answer'] ] ) ? trim( $row[ $atts['answer'] ] ) : '';

        if ( '' === $q || '' === $a ) {
            continue;
        }

        $html .= '<details class="acf-faq-item"><summary>' . esc_html( $q ) . '</summary><div class="acf-faq-answer">' . wp_kses_post( wpautop( $a ) ) . '</div></details>';
        $schema['mainEntity'][] = array(
            '@type'          => 'Question',
            'name'           => wp_strip_all_tags( $q ),
            'acceptedAnswer' => array( '@type' => 'Answer', 'text' => wp_kses( $a, array( 'a' => array( 'href' => true ), 'strong' => array(), 'em' => array(), 'br' => array(), 'p' => array(), 'ul' => array(), 'ol' => array(), 'li' => array() ) ) ),
        );
    }

    if ( ! $schema['mainEntity'] ) {
        return '';
    }

    return $html . '</div><script type="application/ld+json">' . wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG ) . '</script>';
} );
```

## Notes

Create a repeater named faqs with question and answer sub fields (or change the names). Needs ACF PRO.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "FAQ Section With Schema From an ACF Repeater" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
