Title: Primary category
Last modified: May 19, 2026

---

# Primary category

 *  Resolved [shelleyweb](https://wordpress.org/support/users/espressivo/)
 * (@espressivo)
 * [2 months, 3 weeks ago](https://wordpress.org/support/topic/primary-category-18/)
 * **Subject: Fatal error in breadcrumbs when product is missing primary category
   meta**
 * Hi Rank Math team,
 * Wanted to flag a reproducible fatal in the WooCommerce product breadcrumb output.
 * **Plugin:** Rank Math SEO
   **File:** `includes/frontend/class-breadcrumbs.php`**
   Line:** 147 (calls `esc_url()`)
 * **Error:**
 *     ```wp-block-code
       Uncaught TypeError: ltrim(): Argument #1 ($string) must be of type string, WP_Error given
       in wp-includes/formatting.php:4496
       ```
   
 * **Stack trace (abbreviated):**
 *     ```wp-block-code
       #0 wp-includes/formatting.php(4496): ltrim()
       #1 seo-by-rank-math/includes/frontend/class-breadcrumbs.php(147): esc_url()
       #2 seo-by-rank-math/includes/frontend/class-shortcodes.php(73): get_breadcrumb()
       ```
   
 * **Root cause:**
 * When a WooCommerce product is missing the `rank_math_primary_product_cat` post
   meta (e.g. legacy products created before Rank Math was installed, products migrated
   from Yoast or another SEO plugin, or products whose primary category was never
   explicitly set), the breadcrumb builder calls `get_term_link()` and receives 
   a `WP_Error` object back. That `WP_Error` is then passed unchecked into `esc_url()`,
   which calls `ltrim()`, which throws the TypeError under PHP 8.x — fataling the
   entire product page.
 * Related warnings appear in the same stack on affected products:
 *     ```wp-block-code
       class-product.php:96&nbsp;  — Attempt to read property "parent" on null
       class-product.php:100 — Attempt to read property "term_id" on null / Undefined array key 0
       class-product.php:105 — Attempt to read property "name" on null / Undefined array key 0
       class-breadcrumbs.php:532/534 — similar null-property accesses
       ```
   
 * These all stem from the same condition: the primary category lookup returns nothing,
   and downstream code assumes a valid term object.
 * **Impact:**
 * On one site I looked at, ~280 products were missing the primary category meta.
   Every front-end visit to one of those product URLs returned a 500 to the user,
   search engine crawlers, and (in this case) the site’s backup/management service’s
   sync bot — which is how the issue was originally discovered.
 * **Suggested fix:**
 * At `class-breadcrumbs.php:147`, guard against `WP_Error` and null term objects
   before passing the link to `esc_url()`. Either fall back to the parent category,
   the shop page, or simply omit the broken crumb — anything is better than a hard
   fatal on a public product page.
 * For reference, a small mu-plugin filter on `rank_math/frontend/breadcrumb/items`
   that drops crumbs whose URL value is a `WP_Error` is enough to neutralize the
   fatal on the user side, but it would be much better to handle this defensively
   inside the plugin so the warnings/notices also go away.
 * Thanks for the great plugin!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Support [Jaideep Asrani](https://wordpress.org/support/users/jaideeprm/)
 * (@jaideeprm)
 * [2 months, 2 weeks ago](https://wordpress.org/support/topic/primary-category-18/#post-18914391)
 * Hello [@espressivo](https://wordpress.org/support/users/espressivo/),
 * Thank you for contacting Rank Math support.
 * We are not able to reproduce the issue on our end. The breadcrumbs work fine 
   and don’t trigger any error even if the primary category is not set. Since the
   issue is specific to your setup, please check this thread to get in touch with
   our development team, so they can check this further: [https://wordpress.org/support/topic/attention-read-this-before-posting/](https://wordpress.org/support/topic/attention-read-this-before-posting/)
 * Looking forward to helping you.
 *  Plugin Support [Rakibuzzaman Siam](https://wordpress.org/support/users/rakibuzzamansiam1/)
 * (@rakibuzzamansiam1)
 * [2 months, 2 weeks ago](https://wordpress.org/support/topic/primary-category-18/#post-18917522)
 * Hello [@espressivo](https://wordpress.org/support/users/espressivo/),
 * We have already replied to your email and will close this thread for now. Please
   reply to our email so we can continue investigating the issue further.
 * If you would like, you can also reopen this thread or create a new one, and we
   will be happy to assist you.
 * Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)

You must be [logged in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fprimary-category-18%2F%3Foutput_format%3Dmd&locale=en_US)
to reply to this topic.

 * ![](https://ps.w.org/seo-by-rank-math/assets/icon.svg?rev=3438330)
 * [Rank Math SEO – AI SEO Tools to Dominate SEO Rankings](https://wordpress.org/plugins/seo-by-rank-math/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/seo-by-rank-math/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/seo-by-rank-math/)
 * [Active Topics](https://wordpress.org/support/plugin/seo-by-rank-math/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/seo-by-rank-math/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/seo-by-rank-math/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [Rakibuzzaman Siam](https://wordpress.org/support/users/rakibuzzamansiam1/)
 * Last activity: [2 months, 2 weeks ago](https://wordpress.org/support/topic/primary-category-18/#post-18917522)
 * Status: resolved