Using ACF fields on archive pages of custom taxonomy of custom post type

#469893
  • Resolved Aliaksandr Tsykin
    Rank Math free

    Hello there. I need some with a very specific issue.

    I have a custom post type and custom taxonomy for this post type.

    What i wanna do: Use ACF fields on archive pages for this custom taxonomy of custom post type.

    Specifically I want to use data from AFC fields to specify SEO title and SEO description for those pages.

    So basically it’s like this:
    -> Custom Post Type (/fotograf/)
    -> Custom Taxonomy (/miasta/ and /sesja/)
    -> Archive for Custom Taxonomy (url example: https://astrastudio.pl/miasto/gdansk/)
    -> SEO title for this page that I wanna be able to set in Rank Math

    I have tried using following code by adding it with “My Custom Functions” plugin, but it’s either not working or breaks the website if modified:
    /**
    * 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(
    'seo_title',
    [
    'name' => esc_html__( 'SEO title', 'rank-math' ),
    'description' => esc_html__( 'This is SEO title from ACF', 'rank-math' ),
    'variable' => 'seo_title',
    'example' => 'seo_title_callback()',
    ],
    'seo_title_callback()'
    );
    });

    function seo_title_callback(){
    $seo_title = get_field('seo_title', 'term_'.get_queried_object()->term_id);

    return $seo_title;

    I know it’s a lot, so here’s the url example to the archive page that I want to do this on: https://astrastudio.pl/miasto/gdansk/

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

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

    We might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Hello,

    To perform this kind of changes we would need admin access to the dashboard to be able to edit the taxonomy and check that we implementing the code correctly.

    Looking forward to hearing back from you.

    I have updated user access, now it’s admin.

    Hello,

    There has been some syntax issue in the code so I went ahead and fixed it.

    Another issue is that you are using %customfield(seo_title)% variable and you are creating another custom one so %seo_title% should work without %customfield(...)%.

    custom field

    However, the %seo_title% is an in-built variable in our plugin so I created a different name which is now:

    %as_seo_title% for the SEO title field.

    %as_seo_description% for the SEO description field.

    Here’s the complete code:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
    	rank_math_register_var_replacement(
    		'as_seo_title',
    	[
    		'name' => esc_html__( 'SEO title', 'rank-math' ),
    		'description' => esc_html__( 'This is SEO title from ACF', 'rank-math' ),
    		'variable' => 'as_seo_title',
    		'example' => as_seo_title_callback(),
    	],
    		'as_seo_title_callback'
    	);
    });
    function as_seo_title_callback(){
    	$seo_title = get_field('seo_title', 'term_'.get_queried_object()->term_id);
    	return $seo_title;
    }
    
    add_action( 'rank_math/vars/register_extra_replacements', function(){
    	rank_math_register_var_replacement(
    		'as_seo_description',
    	[
    		'name' => esc_html__( 'SEO title', 'rank-math' ),
    		'description' => esc_html__( 'This is SEO title from ACF', 'rank-math' ),
    		'variable' => 'as_seo_description',
    		'example' => as_seo_description_callback(),
    	],
    		'as_seo_description_callback'
    	);
    });
    function as_seo_description_callback(){
    	$seo_desc = get_field('seo_description', 'term_'.get_queried_object()->term_id);
    	return $seo_desc;
    }

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

    I have tested it out and it’s working like a charm. Thank you so much for your help! ^^

    However when I am trying to publish same ACF field group to one more taxonomy (“Sesje” in my case) they suddenly disappear from everywhere.

    Is it possible to have same filed group on both taxonomies at the same time?
    Or should I instead create separate field group for second taxonomy?

    Hello,

    I tested this on my end and it seems to be working just fine and the filter code I added to generate a custom variable can be also reused for Sesje taxonomy.

    Please ensure that you also have added the custom variables in the global title and description under Rank Math > Titles & Meta.

    If the issue persists, let us know and we will do our best to help you resolve it.

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

The ticket ‘Using ACF fields on archive pages of custom taxonomy of custom post type’ is closed to new replies.