Created
September 23, 2019 17:24
-
-
Save chrispian/889deb9682f2bb34c2ed083a3d21e3bc to your computer and use it in GitHub Desktop.
Set Yoast PRIMARY category as the peralink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FIX PERMALINK OF CATEGORIES MATCHING PRIMARY CATEGORY | |
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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: https://wordpress.org/support/topic/same-product-under-multiple-categories-wrong-permalink/#post-11535196