Skip to content

Instantly share code, notes, and snippets.

@Auke1810
Last active March 27, 2026 16:17
Show Gist options
  • Select an option

  • Save Auke1810/30a19490e53b0ef36feaf2ec6fd2c95a to your computer and use it in GitHub Desktop.

Select an option

Save Auke1810/30a19490e53b0ef36feaf2ec6fd2c95a to your computer and use it in GitHub Desktop.
This PHP snippet ensures that your product feed includes the full category path for the product_type attribute.
<?php
/**
* Generate a full category hierarchy (Parent > Child) for the product feed.
* Priority: Yoast Primary Category > First Assigned Category.
*/
function product_type_default_category( $attributes, $feed_id, $product_id ) {
$primary_term_id = 0;
// 1. Try to fetch the Yoast SEO Primary Category first
if (defined('WPSEO_VERSION')) {
$primary_term_id = get_post_meta($product_id, '_yoast_wpseo_primary_product_cat', true);
}
// 2. Fallback: If no Yoast primary is set, grab the first assigned category
if (!$primary_term_id) {
$terms = get_the_terms($product_id, 'product_cat');
if ($terms && !is_wp_error($terms)) {
$primary_term_id = $terms[0]->term_id;
}
}
// 3. Build the full path if a category ID was found
if ($primary_term_id) {
$full_path = array();
// Get all ancestors (IDs from child to parent)
$ancestors = get_ancestors($primary_term_id, 'product_cat');
// Reverse them to get Parent > Child order
$ancestors = array_reverse($ancestors);
foreach ($ancestors as $ancestor_id) {
$ancestor = get_term($ancestor_id, 'product_cat');
if ($ancestor && !is_wp_error($ancestor)) {
$full_path[] = $ancestor->name;
}
}
// Add the current category name as the final leaf
$current_term = get_term($primary_term_id, 'product_cat');
if ($current_term && !is_wp_error($current_term)) {
$full_path[] = $current_term->name;
}
// Merge the names into a single string separated by ' > '
if (!empty($full_path)) {
$attributes['product_type'] = implode(' > ', $full_path);
}
}
return $attributes;
}
add_filter( 'wppfm_feed_item_value', 'product_type_default_category', 10, 3 );
@Auke1810
Copy link
Copy Markdown
Author

Auke1810 commented Mar 27, 2026

How to Implement Category Hierarchy in Your Product Feed

This PHP snippet ensures that your product feed (specifically for the WP Product Feed Manager plugin) includes the full category path (e.g., Furniture > Seating > Sofas) for the product_type attribute. It prioritizes the Yoast SEO Primary Category if set; otherwise, it automatically detects the first available category and builds the hierarchy from there.

  1. Where to Place the Code
    You have two main options for adding this code to your WordPress site:

Option A: Child Theme's functions.php (Recommended)
Navigate to wp-content/themes/your-child-theme/functions.php via FTP or your hosting file manager and paste the code at the bottom. Note: Never add this to a parent theme, as it will be overwritten when the theme updates.

Option B: Code Snippets Plugin
If you prefer not to touch theme files, install a plugin like "Code Snippets" or "WPCode". Create a new snippet, paste the code, and set it to run everywhere.

  1. The Implementation
    Copy and paste the code

  2. How it Works
    Detection: The script checks if you have selected a "Primary Category" using Yoast SEO. This gives you manual control over the feed output.

Automation: If no primary category is selected, it selects the first category associated with the product.

Traceback: It looks "up" the tree to find all parent categories.

Formatting: It joins them with>, creating a clean breadcrumb string that helps Google and other platforms categorize your products more effectively.

  1. Next Steps
    Regenerate your Feed: After saving the code, go to your WP Product Feed Manager settings and refresh/regenerate your feed to see the changes.

Customization: If you prefer a different separator (like a dash -), simply change the > in the implode function at the end of the code.

Pro Tip: Always make a quick backup of your functions.php file before editing. One missing bracket can trigger a "White Screen of Death," so it's best to be safe!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment