Back to Community

Understanding and Setting the Yoast SEO Primary Category

14 threads Sep 7, 2025 PluginYoast seo

Content

Many WordPress users rely on the Yoast SEO plugin to help manage their site's structure and search engine optimization. A common point of confusion revolves around the plugin's Primary Category feature—what it does, how it works, and how to troubleshoot it when it doesn't behave as expected. This guide breaks down the common issues and solutions based on community discussions.

What is the Primary Category Feature?

The Primary Category feature in Yoast SEO allows you to designate one category as the "main" category for a post or custom post type (like a WooCommerce product) that is assigned to multiple categories. Its primary purpose is to help avoid duplicate content issues by telling search engines which version of the URL is the canonical, or preferred, one. This is often reflected in the canonical URL tag generated by the plugin.

Common Issues and Misconceptions

Based on user reports, several key issues frequently arise:

  • Canonical URL Doesn't Match Primary Category: Users expect the canonical URL to always reflect the primary category's permalink structure. However, the canonical URL is determined by a separate process and is not guaranteed to mirror the primary category's slug.
  • Primary Category Not Respected by Themes/Widgets: The primary category selection is a Yoast SEO feature. Standard WordPress functions, theme elements (like breadcrumbs), or WooCommerce widgets will not automatically use it unless they are specifically coded to do so.
  • Bulk Setting Primary Categories: There is no built-in UI for bulk-setting primary categories. Attempting to do so programmatically requires using the correct WordPress hooks.
  • Custom Taxonomies Require Hierarchy: The primary term feature is only available for hierarchical taxonomies (like categories), not non-hierarchical ones (like tags).

Solutions and Workarounds

1. Programmatically Setting the Primary Category

If you need to set the primary category for many posts programmatically, you must use the correct hook. Community members have found that the wp_insert_post hook is often more reliable for this than other save-post hooks. The meta key to update is _yoast_wpseo_primary_{taxonomy}. For a standard post category, it would be _yoast_wpseo_primary_category. For a WooCommerce product category, it is _yoast_wpseo_primary_product_cat.

Example Code Snippet:

function my_set_primary_category( $post_id ) {
    // Your logic to determine the primary term ID goes here
    $primary_term_id = 15; 
    
    // Update the primary category for standard posts
    update_post_meta( $post_id, '_yoast_wpseo_primary_category', $primary_term_id );
    
    // For WooCommerce products, use:
    // update_post_meta( $post_id, '_yoast_wpseo_primary_product_cat', $primary_term_id );
}
add_action( 'wp_insert_post', 'my_set_primary_category', 10, 1 );

2. Using the Primary Category in Your Theme

To display the primary category in your theme templates or use it in a custom WordPress query, you need to retrieve its data. You cannot use a standard WP_Query by category slug or ID to filter for primary categories. Instead, you must first get the primary category ID from the post meta and then use it.

Example Code Snippet to Display Primary Category Name:

$post_id = get_the_ID();
// Get the primary category ID for a standard post
$primary_cat_id = get_post_meta( $post_id, '_yoast_wpseo_primary_category', true );

if ( $primary_cat_id ) {
    // Get the category object using the ID
    $primary_cat = get_category( $primary_cat_id );
    echo $primary_cat->name;
} else {
    // Fallback: get the first category assigned to the post
    $categories = get_the_category( $post_id );
    if ( ! empty( $categories ) ) {
        echo $categories[0]->name;
    }
}

3. Adding Primary Term Support for Custom Taxonomies

As confirmed by user investigation and a GitHub issue, the primary term feature only works for hierarchical taxonomies. If you have a custom taxonomy and want to use this feature, you must ensure it is registered as hierarchical (similar to categories), not non-hierarchical (like tags). You can then use the wpseo_primary_term_taxonomies filter to add it to the list of supported taxonomies.

Example Code Snippet:

function my_add_primary_taxonomy( $taxonomies ) {
    $taxonomies[] = 'your_custom_taxonomy_slug';
    return $taxonomies;
}
add_filter( 'wpseo_primary_term_taxonomies', 'my_add_primary_taxonomy' );

Important Limitations to Remember

  • The primary category feature does not automatically change the visible permalink of a post or product.
  • It is primarily an SEO feature for specifying canonicalization and is not intended to reorganize how posts appear in category archives generated by your theme or WordPress core. Archive pages typically display a post if it is assigned to that category, regardless of its primary status.
  • For WooCommerce, the "Remove category prefix" setting in Yoast SEO does not apply to product categories. Changing these base slugs can lead to 404 errors and is generally not recommended.

By understanding these nuances and using the provided code examples, you can better control how the Yoast SEO Primary Category feature integrates with your WordPress site.

Related Support Threads Support