How to Use ACF Fields in Schema Generator

#697867
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello, I’m sorry to hear that you’re facing issues with using ACF fields in the Schema Generator. It sounds like you’re trying to generate schema from an ACF field on a different page.

    The code provided in the knowledgebase article is meant to help you generate FAQ schema from ACF repeater fields. If you’re looking to pull in a price for an offer from an ACF field on a different page, you may need to customize the code to suit your specific requirements.

    If you need further assistance with customizing the code or have any other questions, please feel free to assign this ticket to our support team using the available buttons, and we’ll be more than happy to help you out. Thank you for reaching out!

    Sources:
    How to Use ACF Fields in Schema Generator » Rank Math
    How to Use ACF Repeater Fields to Automate FAQ Schema » Rank Math
    How to Integrate Rank Math With Advanced Custom Fields » Rank Math

    Hello,

    Thank you for contacting Rank Math support.

    The guide you are following is for the repeater fields. Please share the Schema and field type you are using so we can suggest the correct filter.

    Looking forward to helping you.

    Anonymous
    Rank Math free

    Is there someone who is going to help me resolve this issue? I appreciate all your help.

    Anonymous
    Rank Math free

    Hi,

    I’m new to schema’s, here is the best I can do to answer your question. I apologize if I didn’t answer correctly. I’m going to use the information from one of our sites and put it through validator.schema.org.

    https://validator.schema.org/#url=https%3A%2F%2Fwww.homeofintegrity.com%2Fplumbing-services%2Fplumber-repair-cost-near-tampa-florida%2F

    I believe the schema is Service and the type is offers. This page you are looking at has it’s own ACF fields but not all the information we want is coming from these ACF fields.

    The second part of the offer is on another page (both of these pages are custom post types) is on this page I’ll put through validator.schema.org.

    https://validator.schema.org/#url=https%3A%2F%2Fwww.homeofintegrity.com%2Foffers%2F39-plumbing-repair-assessment-same-day-service%2F

    Pages like this one which has ACF fields such as “price”, “expiration date” and so on. You will see, however, the price is “0” as the price we want is in an ACF field called “sale price” – because all our offers are sales prices.

    If we can figure out how to set up the code to dynamically grab whatever offer is available on those page on that day and grab the missing information from the ACF fields then we would be very thankful.

    If you want I can create a WordPress login for the Dev version of this site where I am currently working out this issue.

    Hello,

    Apologies for the delay as the ticket you have shared seems to have been stuck in our drafts section for some reason.

    In this case, you may need to register the value into a new variable so you can use it in the schema or metadata.

    Here’s a guide on how to create custom variables in Rank Math:
    https://rankmath.com/kb/variables-in-seo-title-description/#how-to-add-custom-variables

    Once done, you can use that variable in the schema filter to fetch the price data.

    Let us know how this goes.

    Anonymous
    Rank Math free

    Hi,

    Still working on this issue after Christmas break. Here is the custom variable snippet I added to the functions.php file

    /**
     * Action: 'rank_math/vars/register_extra_replacements' - Allows adding extra variables.
     */
    add_action( 'rank_math/vars/register_extra_replacements', function(){
    	rank_math_register_var_replacement(
    		'sale_price',
    		[
    			'name'        => esc_html__( 'Sale price', 'rank-math' ),
    			'description' => esc_html__( 'Sale price of the current product.', 'rank-math' ),
    			'variable'    => 'sale_price',
    			'example'     => 'sale_price_callback()',
    		],
    		'sale_price_callback'
    	);
    });
    
    function sale_price_callback()
    {
        $sale_price = get_queried_object();
        return isset($sale_price) ? $sale_price->count : null;
    }

    I don’t understand where to go next in the RankMath global Titles and Meta settings to add the custom variable.

    Hello,

    This type of customization needs special coding from your end since the rest of the offers are coming from a different page.

    However, you can start with this filter if you are working on a service schema:

    add_filter( "rank_math/snippet/rich_snippet_service_entity", function( $entity ) {
      if (!have_rows('Service Page Content')) {
        return $entity;
      }
    	
    	$offers = [];
    	while (have_rows('Service Page Content')) {
        the_row();
    
    		$offers[] = [
    			'@type' => 'Offer',
    			'priceCurrency' => 'USD',
    			'price' => 'XX.XX',
    		];
      }
    	
    	$entity['offers'] = $offers;
      return $entity;
    });

    The custom variable you have created will also not work in the global settings so you’ll have to use this code if you wish to use the custom variable programmatically or within our filter function:

    
    $thePrice = RankMath\Helper::replace_vars("%sale_price%");

    Hope that helps and please do not hesitate to let us know if you need my assistance with anything else.

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

The ticket ‘How to Use ACF Fields in Schema Generator’ is closed to new replies.