Yoast / wordpress-seo

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

yoast_head_json missing fields in Rest API ( wp-json/wp/v2/ ) when conflicting slugs from CPT and CT #21092

Open pkirk opened 5 months ago

pkirk commented 5 months ago

Please give us a description of what happened

To Reproduce

Step-by-step reproduction instructions

  1. Take a fresh WP
  2. Install a plugin like "CPT UI", create a CPT and a CT with the same slug: I created the CPT "test" and the CT "test" (this one linked to the Page PT); you can also use PHP code register_post_type() and register_taxonomy(), the problem doesn't depend on that plugin.
  3. Create a new entry with the new CPT
  4. Check che REST API https://www.example.com/wp-json/wp/v2/test/ , look at the "yoast_head_json" field, and you'll see that the 3.1 the "title" field is wrong, because it refers to the blog title instead of the CPT title 3.2 og_title, og_description, og_url, article_modified_time, og_image (if you have enabled the featured image with add_theme_support( 'post-thumbnails' ) ) and twitter_misc are missing; plus the "schema" field is wrong.

Expected results

  1. To have all the missing fields

Actual results

  1. They are missing

Screenshots, screen recording, code snippet

You can see the REST API's JSON differences here https://www.diffchecker.com/APAQik3J/ ( on the left the correct WP-JSON, with the CT disabled, on the right the wrong WP-JSON with the CT enabled )

Technical info

Used versions

josevarghese commented 5 months ago

Hi @pkirk

Thanks for using the Yoast SEO plugin and for creating the bug report. I didn't get a clear idea of steps 1 and 2 you mentioned. So, please share with us a video of the steps you have taken to create CPT. It would be helpful for us to reproduce the issue better at our end.

You can use free tools like loom.com or awesomescreenshot.com

Look forward to hearing from you.

pkirk commented 5 months ago

Hi @josevarghese , thanks you all for your work! Now back to the issue:

Both activity can be seen in the video here below https://www.loom.com/share/a91d26c2819c41d5a97d832fa397a0b0?sid=f5278abb-cb1c-4481-a775-16b8bb9d2056 where, on a clean WP installation, after having installed Yoast SEO, I install this "Custom Post Type UI" plugin, I use it to create a new Custom Post Type called "Test", then I use it to create a new Taxonomy called "Test", and for the last step I create a new Post of this "Test" type with some random text.

Please take note that this is not about that plugin, because the same result can be achieved using a register_post_type() and a register_taxonomy() in your functions.php, and I will happily provide you the few lines of code needed instead of using the plugin, if you want.

Thanks again.

josevarghese commented 5 months ago

Hi @pkirk

Thanks for sharing the details. I checked the issue and noticed that the yoast_head_json: is outputting based on theid: and link: which is outputted for the slug as a response for the REST API.

So can you please check what's the link: URL outputted in the REST API response?

We look forward to hearing from you and are happy to help you.

pkirk commented 5 months ago

Hi @josevarghese it's not clear to me what "link" are you asking for, given the fact that I already produced the wp-json REST API response ( https://www.diffchecker.com/APAQik3J/ ), so for the link here below https://www.example.com/wp-json/wp/v2/test/9/ (where "test" is the slug of the Custom Post Type, and 9 is it's id) I'm showing you all the output

{
   "id":9,
   "date":"2024-01-29T10:47:03",
   "date_gmt":"2024-01-29T10:47:03",
   "guid":{
      "rendered":"https:\/\/www.example.com\/?post_type=test&p=9"
   },
   "modified":"2024-01-29T10:47:03",
   "modified_gmt":"2024-01-29T10:47:03",
   "slug":"this-is-just-a-test",
   "status":"publish",
   "type":"test",
   "link":"https:\/\/www.example.com\/test\/this-is-just-a-test\/",
   "title":{
      "rendered":"This is just a test"
   },
   "content":{
      "rendered":"\n<h2 class=\"wp-block-heading\">This is just a test<\/h2>\n\n\n\n<p>This is just a test<\/p>\n",
      "protected":false
   },
   "featured_media":0,
   "template":"",
   "yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Bug Yoast<\/title>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:site_name\" content=\"Bug Yoast\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"CollectionPage\",\"@id\":null,\"url\":\"\",\"name\":\"\",\"isPartOf\":{\"@id\":\"https:\/\/www.example.com\/#website\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.example.com\/#website\",\"url\":\"https:\/\/www.example.com\/\",\"name\":\"Bug Yoast\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.example.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->",
   "yoast_head_json":{
      "title":"Bug Yoast",
      "robots":{
         "index":"noindex",
         "follow":"follow",
         "max-snippet":"max-snippet:-1",
         "max-image-preview":"max-image-preview:large",
         "max-video-preview":"max-video-preview:-1"
      },
      "og_locale":"en_US",
      "og_type":"article",
      "og_site_name":"Bug Yoast",
      "twitter_card":"summary_large_image",
      "schema":{
         "@context":"https:\/\/schema.org",
         "@graph":[
            {
               "@type":"CollectionPage",
               "@id":null,
               "url":"",
               "name":"",
               "isPartOf":{
                  "@id":"https:\/\/www.example.com\/#website"
               },
               "inLanguage":"en-US"
            },
            {
               "@type":"WebSite",
               "@id":"https:\/\/www.example.com\/#website",
               "url":"https:\/\/www.example.com\/",
               "name":"Bug Yoast",
               "description":"",
               "potentialAction":[
                  {
                     "@type":"SearchAction",
                     "target":{
                        "@type":"EntryPoint",
                        "urlTemplate":"https:\/\/www.example.com\/?s={search_term_string}"
                     },
                     "query-input":"required name=search_term_string"
                  }
               ],
               "inLanguage":"en-US"
            }
         ]
      }
   },
   "_links":{
      "self":[
         {
            "href":"https:\/\/www.example.com\/wp-json\/wp\/v2\/test\/9"
         }
      ],
      "collection":[
         {
            "href":"https:\/\/www.example.com\/wp-json\/wp\/v2\/test"
         }
      ],
      "about":[
         {
            "href":"https:\/\/www.example.com\/wp-json\/wp\/v2\/types\/test"
         }
      ],
      "wp:attachment":[
         {
            "href":"https:\/\/www.example.com\/wp-json\/wp\/v2\/media?parent=9"
         }
      ],
      "curies":[
         {
            "name":"wp",
            "href":"https:\/\/api.w.org\/{rel}",
            "templated":true
         }
      ]
   }
}

where I can see that has the same problems I mentioned: og_image missing, etc. etc.

Thanks.

josevarghese commented 4 months ago

Hi @pkirk

Thanks for the details. I can replicate the issue you have pointed out.

Can you please also try to open the same REST API URL https://www.example.com/wp-json/wp/v2/test/9/ on an incognito window (without login to your website dashboard and after enabling the wp debug log display on your staging site) or within the error log to confirm whether you notice any errors or not when this odd behaviour occurs?

If you notice any error like " Trying to get property" or anything else, please share it with us.

pkirk commented 4 months ago

@josevarghese , I confirm that I notice errors, and you can find those here below:

<br /> <b>Notice</b>: Trying to get property 'taxonomy' of non-object in <b>/var/www/example/example.com/wp-content/plugins/wordpress-seo/src/presentations/indexable-term-archive-presentation.php</b> on line <b>184</b><br /> <br /> <b>Notice</b>: Trying to get property 'taxonomy' of non-object in <b>/var/www/example/example.com/wp-content/plugins/wordpress-seo/src/presentations/indexable-term-archive-presentation.php</b> on line <b>190</b><br /> <br /> <b>Notice</b>: Trying to get property 'taxonomy' of non-object in <b>/var/www/example/example.com/wp-content/plugins/wordpress-seo/src/presentations/indexable-term-archive-presentation.php</b> on line <b>155</b><br />

josevarghese commented 4 months ago

Thanks for the confirmation. I also noticed the same error, that's why I asked it to confirm it. I reported this issue to our development team.

What's next? Our product team will assess the severity of this problem in relation to other open bug reports and new features. Based on their assessment, the bug report will be given a priority level. Our developers work on the highest priority issues first. Unfortunately, this means we cannot give you an estimate of when they'll start working on your report.

josevarghese commented 4 months ago

Slack for developers (internal reference): https://yoast.slack.com/archives/C5SUKMF2T/p1707983495470909

dimmu123 commented 2 weeks ago

Hi! I have the same issue. Did anyone found a fix?