Yoast / wordpress-seo

Yoast SEO for WordPress
https://yoast.com/wordpress/plugins/seo/
Other
1.76k stars 889 forks source link

%%cf_<custom-field-name>%% doesn't work on a category page #8632

Open benvaassen opened 6 years ago

benvaassen commented 6 years ago

Please give us a description of what happened.

The advanced variable %%cf_%% doesn't show anything. category

Please describe what you expected to happen and why.

I expect to see the content of my custom field on that category in the title in my Snippet Preview.

How can we reproduce this behavior?

  1. Install ACF.
  2. Create a new custom field: acf_category
  3. Set the location: location
  4. Create a new category
  5. Fill out the required fields and enter something in your new custom field
  6. Use the %%cf_acf_category%% for your title
  7. See the Snippet Preview doesn't show the content of the custom field and neither does the frontend

Technical info

mariusghitulescu commented 6 years ago

Please inform the customer of conversation # 281195 when this conversation has been closed.

benvaassen commented 6 years ago

Please inform the customer of conversation # 281195 when this conversation has been closed.

mariusghitulescu commented 6 years ago

I believe the same issue persists on other taxonomies as well. I have just tested in on a Brand taxonomy page (built with Perfect WooCommerce Brands) and custom field variables do not get replaced with their values (neither in the Snippet Editor nor the HTML of the page).

mariusghitulescu commented 6 years ago

Please inform the customer of conversation # 297727 when this conversation has been closed.

suascat commented 6 years ago

Please inform the customer of conversation # 314267 when this conversation has been closed.

asolopovas commented 6 years ago

Any updates on this? I think this has been a problem for a long time.

asolopovas commented 6 years ago

The problem is inside inc/class-wpseo-replace-vars.php on line 679

    private function retrieve_cf_custom_field_name( $var ) {
        global $post;
        $replacement = null;

        if ( is_string( $var ) && $var !== '' ) {
            $field = substr( $var, 3 );
            if ( ( is_singular() || is_admin() ) && ( is_object( $post ) && isset( $post->ID ) ) ) {
                $name = get_post_meta( $post->ID, $field, true );
                if ( $name !== '' ) {
                    $replacement = $name;
                }
            }
        }

        return $replacement;
    }

%%cf_<custom-field>%% will not work as it is not designed to work with the possible solution would be add additional if statements for is_tax() is_archive() etc and add relevant function to retrieve taxonomy and archive custom fields using get_term_meta function

expionage commented 6 years ago

Hi there,

This issue has been ongoing since the start of the year. Is there any progress on it? Looking to implement this as soon as possible in one of my projects.

Thanks!

edemirler commented 6 years ago

Any progress on the issue? I cannot use %%cf_%% on custom taxonomies...

lfriedman74 commented 6 years ago

Same issue. On a custom category page, trying to put custom field into Yoast meta description.

Added custom field "medium" via ACF:

screen shot 2018-06-29 at 10 14 33 am

screen shot 2018-06-29 at 10 15 15 am

Content shown in snippet preview is a:1:{i:0,s:20:"Fine Art Photography";} but should just read Fine Art Photography.

Description in source code reads "Array".

screen shot 2018-06-29 at 10 15 49 am

WP: 4.9.6 Yoast: 7.7.2 ACF Content Analysis for Yoast SEO: 2.0.1 ACF: 4.4.12

pansotdev commented 6 years ago

I have same prob with acf field on custom taxonomy archive. Any info about this? Is been a long time ago since that issue not resolved.

Jerparx commented 5 years ago

Same issue. When putting only %%cf_acf_category%% in the SEO title in the meta box snippet, the output defaults to Title Separator Site title, the same output if you leave the SEO title blank. Therefore, %%cf_acf_category%% is not outputting anything.

image image

Yoast SEO Premium 9.0.3 WordPress 4.9.8 ACF 5.7.7

eroffa commented 4 years ago

The problem is inside inc/class-wpseo-replace-vars.php on line 679

  private function retrieve_cf_custom_field_name( $var ) {
      global $post;
      $replacement = null;

      if ( is_string( $var ) && $var !== '' ) {
          $field = substr( $var, 3 );
          if ( ( is_singular() || is_admin() ) && ( is_object( $post ) && isset( $post->ID ) ) ) {
              $name = get_post_meta( $post->ID, $field, true );
              if ( $name !== '' ) {
                  $replacement = $name;
              }
          }
      }

      return $replacement;
  }

%%cf_<custom-field>%% will not work as it is not designed to work with the possible solution would be add additional if statements for is_tax() is_archive() etc and add relevant function to retrieve taxonomy and archive custom fields using get_term_meta function

Thank you for the tip.

I have a custom section with acf fields added, which in no way wanted to be displayed in yoast.

After some research, I was able to map the value of the acf fields to yoast fields as follows:

  1. In the folder with the plugin, I opened the file inc/class-wpseo-replace-vars.php
  2. Edited the method retrieve_ct_custom_tax_name

    private function retrieve_ct_custom_tax_name( $var, $single = false ) {
        $replacement = null;
    
        $tax  = substr( $var, 3 ); // name field
    
        if ( ( is_string( $var ) && $var !== '' ) && ! empty( $this->args->ID ) ) {         
            $name = $this->get_terms( $this->args->ID, $tax, $single );
            if ( $name !== '' ) {
                $replacement = $name;
            }
        } else {
            $term = get_term_by( 'slug', get_query_var( 'explore_region' ), 'region' );
            $field = get_field( $tax, $term ); // field data
            $name = $field['region_seo_type'];
    
            if ( $name !== '' ) {
                $replacement = $name;
            }
        }
    
        return $replacement;
    }
  3. I went into the user category and substituted my acf field in the template: %%ct_region_seo_group%%. Where region_seo_group is acf group type field.
  4. After saving the category, reset the permalinks, cleared the cache and the data from the user field began to display correctly.

P.S. Everything works even without the additional acf-content-analysis-for-yoast-seo plugin

monbauza commented 1 year ago

Issue still relevant in Yoast SEO 20.8. I tried to add a snippet variable for an ACF custom field, i.e. second_description, to the meta description of a category page; however, after saving the page, the variable I added %%cf_second_description%% got replaced with %%_second_description%% which is then output in the frontend. See screencast below.

https://github.com/Yoast/wordpress-seo/assets/9658774/52ba2c9c-04ae-4f0c-8e5b-67c6f4271446

monbauza commented 1 year ago

Please inform the customer of conversation # 1023742 when this conversation has been closed.

monbauza commented 1 year ago

Probably related https://github.com/Yoast/wordpress-seo/issues/20487

kassimariemc commented 1 year ago

Issue still relevant in Yoast SEO 20.8. I tried to add a snippet variable for an ACF custom field, i.e. second_description, to the meta description of a category page; however, after saving the page, the variable I added %%cf_second_description%% got replaced with %%_second_description%% which is then output in the frontend. See screencast below.

Screen.Recording.2023-06-05.at.12.11.19.88.PM.mp4

Still seeing this issue on the latest release. Is there any path to resolve this?