1. Home
  2. Knowledge Base
  3. SEO Suite
  4. How to Use ACF Fields in Schema Generator

How to Use ACF Fields in Schema Generator

In this knowledgebase article, we’re going to walk you through how to use ACF fields in Rank Math’s Schema Generator.

What is Advanced Custom Fields Plugin?

Advanced Custom Fields, aka ACF, is a WordPress plugin that allows you to add extra content fields known as Custom Fields and helps to tailor what fields to show. Advanced Custom Fields lets you build websites faster and quickly educate your customers.

Using Advanced Custom Fields with Rank Math, you can enrich your pages and blog posts with more data and information with an easy-to-use solution. In addition, it’s effortless to integrate Rank Math with Advanced Custom Fields.

How to Use ACF Fields in Schema Generator?

The custom field variable (%customfield%) will not work on the ACF fields. This is due to how ACF stores values in the post meta table. It stores values in a serialized format for groups, and for other fields, it stores key-value pairs.

You can define a new variable using this filter to get the value from the ACF field and use it in the Schema Generator. Here’s how we register the WooCommerce variable in the Rank Math plugin.

We recommend using the rank_math/json_ld filter to generate FAQ schema on the front end instead of adding it from the Schema generator.

You can do this by adding the following code to your theme’s functions.php or rank-math.php file:

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

The first step is to navigate to WordPress Dashboard > Appearance > Theme File Editor (for a Classic Theme).

Navigate to Theme File Editor

For a Block Theme like Twenty Twenty- Two, navigate to WordPress Dashboard > Tools > Theme File Editor.

Theme File Editor in Block Theme

Now select the theme’s functions.php or rank-math.php file.

Then add the above code and click the Update File button.

Also, please ensure that you change the acf-repeater-faq, faq_question, faq_answer fields as per the settings in the ACF:

FAQ Fields

You can also read our knowledgebase article How to Use ACF Repeater Fields to Automate FAQ Schema to help you add and manage custom fields easily. If you still have any questions about how to use ACF fields in the Schema Generator – you’re always more than welcome to contact our dedicated support team.

Was this article helpful?

Still not using Rank Math?

Setup takes less than 5 minutes including the import from your old SEO Plugin!

Learn more about the PRO Version

Still need help?

?

Submit Your Question

Please give us the details, our support team will get back to you.

Open Ticket

Related Articles