BUG | ACF Fields do not work for Meta Description

#993385
  • Resolved Michael Pucher
    Rank Math free

    Hi,

    on a CPT I obligatory need to use the value of an ACF field as content for “Meta Description” (and not the value of the native CPT excerpt field).

    By default in Rank Math “Title & Meta”, in the settings panel of this very CPT, I do have my “Meta Description” field.

    Situation 1 – %excerpt%
    =======================
    If I am using %excerpt% and there is a value in excerpt field => I off course get the excerpt content as Meta Description => perfectly fine

    Situation 2 – %customfield(leistungen_-_hero_subline)%
    ======================================================
    But since here on this client site, we are using (for several good reasons) the value of the excerpt field intentionally NOT as a SEO Meta Description, but for another usecase. Because of this we want to use the value of an ACF Intro Text field “%customfield(leistungen_-_hero_subline)%” as a value for the SEO Meta Description … so we would like to achieve:
    1) WordPress CPT Excerpt Field => must not be used for Meta Description
    2) ACF Intro Text Field => must be used for Meta Description on this site

    Problem:
    Since we do have content in the CPT excerpt field, Rank Math ALWAYS pulls the value of the excerpt field for the Meta Description, even if I add “%customfield(leistungen_-_hero_subline)%” in the Settings Panel, Meta Description field.

    Only if the native excerpt field is empty, Rank Math correctly pulls the content of my ACF field. But as soon as there is content in the native excerpt field, Rank Math ignores my ACF field content for the meta description.

    Is there a quick solution for this bug?

    Thanks and best regards
    Michael

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

    We apologize for the issue you are facing with the ACF fields not working as expected for the Meta Description in Rank Math. It seems that Rank Math prioritizes the native excerpt field over custom fields when generating the Meta Description, which is causing the behavior you described.

    Currently, the custom field variable (%customfield%) does not work directly for ACF fields in the Meta Description due to how ACF stores values. Rank Math will use the excerpt if it is available, and only fall back to the custom field if the excerpt is empty.

    To achieve your desired outcome, you can define a new variable using a filter to get the value from the ACF field and use it in the Meta Description. Here’s a general approach:

    1. Add a filter in your theme’s functions.php file to create a custom variable that retrieves the ACF field value.
    2. Use this custom variable in your Meta Description settings instead of %customfield(leistungen_-_hero_subline)%.

    Here’s an example of how you might set this up:

    add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
        $data['metaDescription'] = get_field('leistungen_-_hero_subline'); // Replace with your ACF field name
        return $data;
    }, 10, 2 );
    1. Update your Meta Description settings to use this new variable.

    This way, you can ensure that the ACF field value is used for the Meta Description regardless of the content in the native excerpt field.

    If you need further assistance with implementing this solution or if you encounter any issues, please reply to this ticket, and a human agent from our support team will assist you further.

    Thank you for bringing this concern to our attention!

    Hi Human Person,

    I did not test the above solution, but – from my point of view – this should be natively implemented into Rank Math Core …

    Hmmm … what do you think?

    Hello,

    You can set the custom variable in Rank Math > Titles & Meta settings and after that use the following filter code to force Rank Math to use that:

    add_action( 'rank_math/frontend/description', function( $description ) {
     global $post;
     $desc = RankMath\Post::get_meta( 'description', $post->ID );
    
     $post_type = get_post_type(get_queried_object());
    
     if (($post_type == 'your_post_type')) { //change 'your_post_type' with the actual CPT
     $desc = RankMath\Helper::get_settings( "titles.pt_{$post->post_type}_description" );
     if ( $desc ) {
     return RankMath\Helper::replace_vars( $desc, $post );
     }
     }
    
     return $description;
    });
    

    Here’s how you can add filter/hook to your WordPress site: https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Let us know how it goes. Looking forward to helping you.

    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.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘BUG | ACF Fields do not work for Meta Description’ is closed to new replies.