Skip Navigation

[Resolved] Using Yoast SEO Primary Category as filter in Views

This thread is resolved. Here is a description of the problem and solution.

Problem: In a View of Products, I would like to nest another View of Products that is filtered by the same Primary Category as the current post in the loop. The Primary Category is a feature from Yoast SEO.

Solution: Yoast's Primary Category is stored as a term ID in a hidden custom field _yoast_wpseo_primary_product_cat on the Product post. You can set up a View of Products filtered by term slug set by a shortcode attribute, convert the term ID into a term slug with PHP, and pass the term slug into a PHP render_view function.

This support ticket is created 2 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by per-arneF-2 2 years, 6 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#1642571

I'm trying to create a view listing all posts that have the same Main Category as the current post in the loop (Set by Yoast SEO). However, I can't seem to find a way to filter on Main Category. Is it possible?

#1645531

Hi, I think you are referring to Yoast's primary category. Views doesn't directly integrate with that feature, but basically Yoast adds a hidden custom field to each post where the ID of the "primary" category for that post is stored. I can show you how to filter a View of posts by taxonomy, using the value of the current post's hidden field value as the term ID.

Here's another similar ticket: https://toolset.com/forums/topic/present-one-product-category-on-product-page/

First, create an additional View of this post type. In the popup that appears when you create the View, choose "Display all the results" or "full custom display". In the View editor, choose the appropriate post type and add a taxonomy term Query Filter, using the Product Category taxonomy. Configure that filter to respond to a shortcode attribute like "term". This will allow you to pass the primary category ID into the View using a shortcode attribute, so the filter is dynamic. Once that's done, scroll down to the Loop editor and add some content for testing. For now, please add the post title with a link followed by the wpv-post-field shortcode to display the primary category ID:

[wpv-post-link], Primary category ID: [wpv-post-field name="_yoast_wpseo_primary_product_cat"]<br />

Now insert this nested post View inside the loop of the parent post View. You must remember to insert the wpv-post-field shortcode in the term attribute, so the Query Filter will work correctly. I also inserted the shortcode to display the outer post's primary category ID, for testing purposes. Here is an example of the syntax:

Outer loop post primary category ID: [wpv-post-field name='_yoast_wpseo_primary_product_cat']<br />
[wpv-view name="Your Nested View of Similar Posts" term="[wpv-post-field name='_yoast_wpseo_primary_product_cat']"]

Please let me know if you have problems implementing this solution and I can take another look.

#1645551
Skjermbilde 2020-05-31 kl. 19.16.32.png

I get the idea. In this case the field name is _yoast_wpseo_primary_category

The problem I have now, is that I can't seem to find a way to filter by taxonomy-id, just by slug and name. I must be overlooking something 🙁

#1645573

Fixed it by referencing in the theme file.
$args = array(
'title' => 'Name of View',
'term' => $category_display // Name of category from _yoast_wpseo_primary_category
);
echo render_view( $args );
}