Custom Fields
Re-Envisioned
Support MB Views How do display CPT with selected taxonomy anywhere on the site?

  • Creator
    Topic
  • #21498
    Resolved Adam
    Participant

    I 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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #21499
    Adam
    Participant

    Sorry, 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

    #21507
    Long Nguyen
    Moderator

    Hi 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.

    #21509
    Adam
    Participant

    Hrm… 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

    #21520
    Long Nguyen
    Moderator

    Hi,

    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.