I have a custom post type called Athletes.
In each athlete I would like to be able to select a WooCommerce product category for that Athlete.
Using that data I can then post that athletes latest products on their single template page and have a button to link to their full shop category listing.
So the athlete post wont 'belong' to the shop product category but it will link to it.
Is that possible? Many thanks.
Hi, what if you store the corresponding product category slug as a single line text field on the Athlete post? It will be easier than trying to maintain a select field populated by all the dynamic product categories in WooCommerce. For example, create a single-line text custom field on the Athletes post type called "pcat-slug". Whenever you create an Athlete post, save the slug of their corresponding product category. Then build your link to the Product Category archive using the slug something like this:
<a href="<em><u>hidden link</u></em> name='wpcf-pcat-slug']">See all [wpv-post-title]'s Products</a>
That will allow you to use the Athlete's pcat-slug field in a View shortcode attribute. This way you can create a View of Products filtered by Product Category term slug, something like this in your Athlete template:
[wpv-post-title]'s Latest Products:<br />
[wpv-view name="Your Latest Product View" wpvproductcat="[wpv-post-field name='wpcf-pcat-slug']"]
This technique is called passing arguments into Views. More info about this:
https://toolset.com/documentation/user-guides/passing-arguments-to-views
Thanks! That would work really well.
I guess the only downside is exactly as you say - it wont give me a nice dropdown to choose from on the admin site, would have to look up the slug and type it manually.
I was able to find some code posted by another user on this forum....
add_action( 'init', 'add_product_cat_to_custom_post_type',11 );
function add_product_cat_to_custom_post_type() {
register_taxonomy_for_object_type( 'product_cat', 'athlete' );
}
This let me assign a product_category to an athlete post. Then I used hidden link plugin to ensure only one category could be selected.
This seems to have done the trick but I don't know if WooCommerce will get upset by the custom post type belonging to a category that is designated for products. So far all seems well but I will report back if it breaks anything.
Thanks again
Sounds like you have a custom code approach. I'll stand by for your update.