Yoast / wordpress-seo

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

Using the %%pt_plural%% on a taxonomy archive results in error #9539

Closed rmarcano closed 5 years ago

rmarcano commented 6 years ago

Please give us a description of what happened.

Trying to use the %%pt_plural%% or %%pt_single%% template variable on a taxonomy archive results in the following error on the page:

Notice: Trying to get property of non-object in /home/XXXX/public_html/test/wp-content/plugins/wordpress-seo-premium/inc/class-wpseo-replace-vars.php on line 639

Notice: Trying to get property of non-object in /home/XXXX/public_html/test/wp-content/plugins/wordpress-seo-premium/inc/class-wpseo-replace-vars.php on line 640

Please describe what you expected to happen and why.

The post type label to be echoed on the page where I use the template variable.

How can we reproduce this behavior?

1.Edit a Category archive page to include the %%pt_plural%% in the title field for example. 01

2.Check the category page. See error. 02

Technical info

rmarcano commented 6 years ago

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

TimVevida commented 6 years ago

FYI: I experienced this problem when trying to set the title for all categories of a custom post type, using the Yoast settings screen (Search Appearance > Taxonomies > MyCustomPostType > Title template*)

* I think, I am using the Dutch translation, so I am not sure the naming here is correct.

TimVevida commented 6 years ago

Note: Since Yoast 7.7, there is no error on the page anymore. However, the single/plural post type name is still not shown.

moorscode commented 5 years ago

This is not a realistic way of using the %%pt_plural%% or %%pt_singular%% replacement variables. A taxonomy can contain items from multiple post types, thus there is not one unique post type that applies.

The way to achieve what you are seeking for is to just hard-code the text that you want to have appear in front of the name of the taxonomy:

screen shot 2018-12-11 at 15 56 08
TimVevida commented 5 years ago

Hm, but I want to localize my text. This is not possible using the hard-coded approach.

I am using a custom taxonomy that is only used for a single post type.

Can't you tell if a taxonomy archive page is restricted to (child of) a single post type and echo the %%pt_plural%% only then?

radeno commented 3 years ago

This was able up to major version 13, but from version 14 it is not able to put %%pt_single%% or %%pt_plural%%

For some of our projects, it is an unhappy situation because we are prepending taxonomies which are connected only with one post type.

Is there any option on how to get post types works with taxonomies for title generating?

I am attaching screenshots where is Query Monitor debug for the main query. We can see that for specific URL post type and taxonomy are recognized correctly by WordPress. But WP SEO for this configuration can't recognize post type or taxonomy properly and the title is generated probably by default WordPress because we are using pipe | in WP SEO config but dash - is used for the title.

Screen Shot 2020-11-22 at 13 43 49

Screen Shot 2020-11-22 at 13 34 03

We are using this code to create URLs combination post-type and taxonomy

public static function addPostTaxonomiesRewrite($postType, $taxonomies)
    {
        $postTypeObject    = \get_post_type_object($postType);
        $taxonomiesObjects = array_map(function ($taxonomy) {
            return \get_taxonomy($taxonomy);
        }, $taxonomies);

        $postTypeRewriteSlug          = $postTypeObject->rewrite['slug'];
        $taxonomiesRewriteSlugsString = join('/', array_map(function ($taxonomy) {
            return $taxonomy->rewrite['slug'] . '/' . '(.+?)';
        }, $taxonomiesObjects));

        $taxonomiesRedirectString = join('&', array_map(function ($taxonomy, $i) {
            $_i = $i + 1;
            return "{$taxonomy}=\$matches[{$_i}]";
        }, $taxonomies, array_keys($taxonomies)));

        $regexPaged       = "{$postTypeRewriteSlug}/{$taxonomiesRewriteSlugsString}/page/([0-9]{1,})?$";
        $matchesIncrement = count($taxonomies) + 1;
        $redirectPaged    = "index.php?post_type={$postType}&{$taxonomiesRedirectString}&paged=\$matches[{$matchesIncrement}]";
        \add_rewrite_rule($regexPaged, $redirectPaged, 'top');

        $regex    = "{$postTypeRewriteSlug}/{$taxonomiesRewriteSlugsString}/?$";
        $redirect = "index.php?post_type={$postType}&{$taxonomiesRedirectString}";
        \add_rewrite_rule($regex, $redirect, 'top');
    }