Using custom post fields option page values?

#73210
  • Resolved Johan W
    Rank Math free

    It seems you can only use custom post fields for the actual page in titles and descriptions.

    But I want to be able to use global custom post fields from option pages.

    Will this be supported?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Alberto
    Rank Math business

    Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    You could create a custom var that gets those fields by using the following filter (you will need to know a little about coding to be able to modify it to fit your needs):

    /**
     * Filter to add custom variables
     */
    add_filter( 'rank_math/vars/replacements', function( $vars ) {
     return $vars;
    });

    Looking forward to helping you. Thank you.

    ​​​​​​​

    Seems rank_math/vars/register_extra_replacements is the function I need.

    But how do I use it with function parameters? This code works for the example in admin, but not live. The value in title will be blank.

    It gets the data from ACF.

    add_action( 'rank_math/vars/register_extra_replacements', function(){
            rank_math_register_var_replacement(
                    'amount_cars',
                    [
                            'name'        => esc_html__( 'Total amount of cars', 'rank-math' ),
                            'description' => esc_html__( 'Output total amount of cars.', 'rank-math' ),
                            'variable'    => 'amount_cars',
                            'example'     => get_field('amount_cars', 'option'),
                    ],
                    get_field('amount_cars', 'option')
                    );
    });

    I tried:
    get_field(‘amount_cars’, ‘option’)
    and
    “get_field(‘amount_cars’, ‘option’)”

    Hello,

    Thank you for the follow up.

    You would need to create the callback function to get the values:

    
    get_field($var1, $var2){
     $var1 = //some_code to get the value;
     $var2 = //some_other_code to get other value
    
    return some_var_here
    }
    

    To get this right,you would need to be a developer or seek help from one.

    Hope that helps. If you have any further question(s), please let us know. Thank you.

    The function already exists in ACF.

    Alberto
    Rank Math business

    Hello,

    I am sorry but I don’t understand what do you mean with “The function already exists in ACF.”. Could you let us know some more details about it?

    In the example my colleague shared, you only need to get the ACF fields you want in that function, edit them as you wish and return them so the custom var will display them.

    Looking forward to help you.

    https://www.advancedcustomfields.com/resources/get_field/

    Why do I need to make one more function?

    Alberto
    Rank Math business

    Hello,

    Because the callback function is the one being called from the Rank Math filter. From there you can call that get_field function, manipulate that text and provide it as a return of the callback function.

    Looking forward to help 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 7 replies - 1 through 7 (of 7 total)

The ticket ‘Using custom post fields option page values?’ is closed to new replies.