skip to Main Content

With Yoast SEO comes a functionality to set primary category among more than one category selection.
I am writing a piece of code where I need to fetch the primary category using the word-press DB tables.
I am unable to identify a way to query the DB to get only the primary category and not any other category associated with the post.

I am using wordpress version 4.5.3 with Yoast SEO installed.

2

Answers


  1. Chosen as BEST ANSWER

    I finally figured it out. The primary category as is a feature of the Yoast SEO plugin is available in wp_postmeta table. Each post's primary category is set against meta_key value "_yoast_wpseo_primary_category" with meta_value having the term_id from the wp_terms table for the primary category.


  2. this query show for posts:

    SELECT * FROM wp_posts t1 
    INNER join `wp_postmeta`t2 
    on t1.ID = t2.post_id
    where meta_key = '_yoast_wpseo_primary_category'
    

    this query show primary category for products:

    SELECT * FROM wp_posts t1 
    INNER join `wp_postmeta`t2 
    on t1.ID = t2.post_id
    where meta_key = '_yoast_wpseo_primary_product_cat'
    

    pay attention that meta_key value on above have different.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search