Yoast / wordpress-seo

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

Primary category not shown in product URL #4528

Closed monbauza closed 7 years ago

monbauza commented 8 years ago

What did you expect to happen?

Product URL should include the category selected as primary

What happened instead?

The "Make Primary" functionality doesn't have any effect on the URLs of products

How can we reproduce this behavior?

Create a product with two categories (i.e. Category A and Category B) and select the second one as primary. The URL will include category-a instead of category-b (see image below)

image image

Technical info

mmikhan commented 8 years ago

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

Arnijholt commented 8 years ago

Exacty the same problem right here. Is there a solution (or work around) available to solve this issue?

Thanks in advance!

Frique commented 8 years ago

Yeah this simply doesn't work. Is it a conflicting WC setting or other plugin or configuration? I've solved it by hooking into a WC filter myself:

add_filter( 'wc_product_post_type_link_product_cat', function( $term, $terms, $post ) {

    // Get the primary term as saved by Yoast
    $primary_cat_id = get_post_meta( $post->ID, '_yoast_wpseo_primary_product_cat', true );

    // If there is a primary and it's not currently chosen as primary
    if ( $primary_cat_id && $term->term_id != $primary_cat_id ) {

        // Find the primary term in the term list
        foreach ( $terms as $term_key => $term_object ) {

            if ( $term_object->term_id == $primary_cat_id ) {
                // Return this as the primary term
                $term = $terms[ $term_key ];
                break;
            }

        }

    }

    return $term;

}, 10, 3 );
monbauza commented 8 years ago

A customer has reported the same issue happening with other custom taxonomies.

See images below. The publication term selected as primary isn't reflected correctly in the permalink. The permalink always shows ' Eastern Reporter' irrespective of the publication term selected as primary.

image image image

monbauza commented 8 years ago

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

mmikhan commented 7 years ago

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

soo7mi commented 7 years ago

I'm experiencing the same exact issue with a custom post type and categories. Is there a fix for this?

atimmer commented 7 years ago

We need to verify if it is possible to do this globally with a solution that works for all taxonomies that might occur in the URL. If so, we can just solve this. If not, we need to build plugin specific solutions and then we probably build a WooCommerce specific solution. Either in Yoast SEO, or Yoast SEO: WooCommerce.

dfumagalli commented 7 years ago

Considering @Frique 's solution perfectly worked, I don't see why not apply it.

Pcosta88 commented 7 years ago

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

Pcosta88 commented 7 years ago

User reports,

I have done a lot of testing, and it seems to be choosing permalink structure based upon "desc alphabetical", meaning, for example, the category "zoo" will get priority over "boo" - even though primary category is "boo"…

en7jos commented 7 years ago

This is odd. The 'Make Primary' category was working fine (I think) until I upgraded to WooCommerce 3.0 this morning. Now the selected primary category has no effect upon the product permalink which is always set as being the alphabetically-first category selected (i.e. categories beginning with a number take precedence to those starting with 'A' and then through to those starting with 'Z').

Adding the code snippet posted by Frique above on 13 Jul 2016 fixes the problem for me so that the selected primary category is actually the one used in the product permalink. Thanks Frique!

JonSymons commented 7 years ago

I believe you are correct en7jos. This problem only showed up for me after upgrade to WC 3.0.

I tried the snippet from Frique, and no luck. Assuming it needs to go in the Theme Functions functions.php page?

en7jos commented 7 years ago

@JonSymons. Yes, it would go in your functions.php, but I prefer to use the 'Code Snippets' plugin as that it a much easier and safer way to add code than messing around with core files, and any tweaks are not lost or affected by plugin or theme updates etc. See here: https://wordpress.org/plugins/code-snippets/

The above code snippet seems to have fixed the problem for me when adding it via the plugin in this way.

Could anyone from Yoast assist or comment please? Thanks.

Pcosta88 commented 7 years ago

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

en7jos commented 7 years ago

@Pcosta88 How do we view conversation 195733 please?

Has this issue been resolved in the latest Yoast updates?

Pcosta88 commented 7 years ago

@en7jos The conversation is part of our ticketing system and is unable to be viewed. The issue is also still an open one and has not been resolved in the latest Yoast version 4.7.1.

jefvantongerloo commented 7 years ago

Any update on this problem? For me the problem still exists.

JonSymons commented 7 years ago

They think they fixed it in a previous release, so I wouldn't hold your breath. The problem still exists for me as well.

nicomollet commented 6 years ago

@Frique snippet seems to work for me.

anujconsagous commented 6 years ago

My primary category is not working , it is working according to alphabetically selection. How it is working and what i do for this please help.

please see widget image to understand https://cloud.githubusercontent.com/assets/9658774/17168795/a82c8c24-53e4-11e6-8ef4-36815073cd4e.png

chilliwax commented 6 years ago

hello, has anyone been able to resolve this problem yet? We have just noticed the same issue but could have been happening for a long time....really not good!

rabdilhamidov commented 6 years ago

Woocomerce 3.3.3, Yoast SEO Premium 4.8, WooCommerce Perfect SEO Url 2.6.5.

The code in https://github.com/Yoast/wordpress-seo/issues/4528#issuecomment-232263154 not working for me too.

I found that WooCommerce Perfect SEO Url plugin have a filter (woocommerce-perfect-seo-url/perfect-seo-url.php:298) with $priority = 1 that override %category% in permalink string before woocommerce add_filter( 'post_type_link'...) (woocommerce/includes/wc-product-functions.php:275). I wrote filter to solve the problem:

add_filter( "psu_post_type_link_replace_term", function($first_term, $terms){
    global $post;

    $primary_cat_id = get_post_meta( $post->ID, "_yoast_wpseo_primary_product_cat", true );

    // If there is a primary and it's not currently chosen as primary
    if ( $primary_cat_id && $first_term->term_id != $primary_cat_id ) {

        // Find the primary term in the term list
        foreach ( $terms as $term ) {

            if ( $term->term_id == $primary_cat_id ) {
                // Return this as the primary term
                $first_term = $term;
                break;
            }
        }
    }
    return $first_term;
}, 10, 2 );
0xjluis commented 5 years ago

Still not working for me

WordPress 5.1.1

WooCommerce version: |   3.5.7

Yoast SEO 10.1.3

beckspaced commented 5 years ago

still the same problem here. the selected primary category isn't showing up in the WC product slug URL perhaps someone could please re-open this issue? that would be great ;)

WordPress 5.1.1 WooCommerce 3.6.4 Yoast 11.3

Djennez commented 5 years ago

Hi, this is functionality that is added to our woocommerce extension.

beckspaced commented 5 years ago

Hi @Djennez thanks a lot for your quick reply. So with the Yoast woocommerce extension the primary category will get displayed in the wc product slug URL? I'm only asking because I don't see that feature mentioned in the link your provided. I see a note about breadcrumb navigation which actually already does display the proper primary category. What I'm lloking for is a fix for the missing primary category on the wc product slug URL.

Sorry for bothering you again ;) thanks for your time & help!

Greetings Becki

Djennez commented 5 years ago

Yea, that's the idea :) This particular issue was closed because of this PR

will-ashworth commented 5 years ago

And for those of us not using WooCommerce, what's the fix?

We utilize an external shopping solution and integrate with them already. We love this functionality introduced to the Yoast SEO plugin ("Make primary"), but the feature feels a little crippled if it only works for the primary taxonomy and not others (ie., custom taxonomies). We'd prefer not to activate the plugin bloat that can be WooCommerce for the website we're working with, and I'm not sure if the Yoast SEO premium plugin will actually work for me without WooCommerce installed and activated.

Perhaps this is an opportunity for a premium Yoast plugin that doesn't specifically relate to WooCommerce or another third-party plugin. Because we didn't get involved with Yoast for WooCommerce help; we starting using it for SEO. Yet, the only solution I see out there for this is something relating to e-commerce. That doesn't make sense to me. 🤔🙃

It would be nice if we had a solution; either open source or paid. People have been trying to solve this one with Yoast for years. Thank you @Djennez for commenting and keeping in touch with us on this as you have. 🔥

Djennez commented 5 years ago

Hi @will-ashworth, for WooCommerce we are able to hook into wc_product_post_type_link_product_cat to change the primary category in the URL. If this is what you're referring to I can only suggest to look for a way to hook into your e-commerce plugin to change the URL. Then, you might be able to alter this code a bit to get the primary category of your product and substitute the category in your URL that way.

Might need some tweaking and developing, but it should be possible to get your plugins primary product category into the URL somehow.

will-ashworth commented 5 years ago

@Djennez -- Thank you! We're just using a product custom post type created ourselves with a product_category custom taxonomy. Ironically, we're probably using close to what you're already account for in WooCommerce...only we're not using that. So we need a different workaround I think.