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?
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.
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 🙁
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 );
}