How do display CPT with selected taxonomy anywhere on the site?
- This topic has 4 replies, 2 voices, and was last updated 5 years, 1 month ago by
Long Nguyen.
-
AuthorPosts
-
August 27, 2020 at 10:33 PM #21498
Adam
ParticipantI have created a custom post type, Testimonials.
I have created a taxonomy called Testimonial Categories which is used by the Testimonials CPT. It includes three categories, Coaching, Growth, and Planning.
I have a Testimonials archive page set up using MB Views and it is correctly displaying all testimonials.
https://www.dropbox.com/s/xh8sriz2glk29m1/archive-view.png
The client would also like testimonials displayed on specific pages depending on the testimonials' selected categories. So only testimonials categorized as Planning, for example, would display on the Planning page. However, I am not sure how to do this. Here is what I currently have set up:
https://www.dropbox.com/s/lncbg3mdam3pw3h/planning-view.png?dl=0
Any idea how I can achieve this?
Thanks!
August 27, 2020 at 10:43 PM #21499Adam
ParticipantSorry, images aren't embedding for me. Here are the links.
Archive: https://www.dropbox.com/s/xh8sriz2glk29m1/archive-view.png
Planning: https://www.dropbox.com/s/lncbg3mdam3pw3h/planning-view.png
August 28, 2020 at 12:30 PM #21507Long Nguyen
ModeratorHi Adam,
To apply the view for a specific term archive, please use the Location > Select Taxonomy Archive > Select a term. See my screenshot https://share.getcloudapp.com/Z4ukDPrD.
August 28, 2020 at 7:11 PM #21509Adam
ParticipantHrm... I figured out the sorting part via a PHP function. However, in the view I want to get it by category. I know my PHP pretty well and could see creating a template file, but I am wanting to accomplish this via the View with Twig.
Here is a bit of code I have that pulls the testimonials, but I am wanting the testimonials that have the category of "planning"
{% set args = { post_type: "testimonials", posts_per_page: 20 } %} {% set posts = mb.get_posts( args ) %} {% for key, post in posts %} <div> <span>{{ post.post_title}}</span> </div> {% endfor %}This grabs just that. But arguments like cat: "planning" and such do not work. Is there an example anywhere I can see to pull in that category?
Thanks
August 29, 2020 at 12:32 PM #21520Long Nguyen
ModeratorHi,
To get posts by custom taxonomy, please use the argument
tax_query. Here is the sample code:{% set args = { post_type: 'testimonials', posts_per_page: 20, tax_query: [ { taxonomy: 'testimonial_category', field: 'slug', terms: 'planning' } ] } %}For more information about arguments, please follow this documentation https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters.
-
AuthorPosts
- You must be logged in to reply to this topic.