Get related items in MB View with tax query

Support MB Views Get related items in MB View with tax queryResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #34497
    info@ceescoenen.nl[email protected]
    Participant

    This code works and get all the locatie's that are related to the current route.

    {% set relationship = attribute( relationships, 'route-to-locatie' ) %}
    {% for post in relationship.to %}
        <p>
            {{ post.title }}
        </p>
    {% endfor %}

    The CPT locaties has terms like horeca or highlight. I will show only the related post that has also the term horeca. I Oxygen builder I have made an Advanced Query like https://ibb.co/JkpPn0B

    'tax_query' => array(
        array(
            'taxonomy' => 'soort_locatie',
            'terms' => 'horeca',
            'field' => 'slug'
        )
    )

    How do I get this in MB View?
    And when I have to use {% set posts = mb.get_posts( args ) %} how do I construct the args based on the info above? (post_type: locaties etc)

    #34498
    info@ceescoenen.nl[email protected]
    Participant

    I think I have found it 🙂 :

    {% set args = {
     post_type: 'locaties', 
     relationship: {id: 'route-to-locatie', from: post.ID},
     tax_query: [{
           'taxonomy': 'soort-locatie',
           'field': 'slug',
           'terms': 'horeca'
          }]
    }%}
    {% set posts = mb.get_posts( args ) %}

    But I don't understand when to use {[]} and when only {}. The same for post_type : 'locaties' vs 'post_type' : 'locaties' ('single quotes' around 'post_type'.

    #34506
    Long NguyenLong Nguyen
    Moderator

    Hi Cees,

    Use brackets, a pair of marks [], when you have an array of values without keys like this
    [value 1, value 2, value 3]

    Use curly brackets, a pair of marks {}, when you have an object or array with keys and values like this
    {a: 'A', key1: 'value 1', key2: 'value 2'}
    key also does not need to wrap in the single quotes.

    Refer to this topic https://support.metabox.io/topic/displaying-two-cpts-in-custom-taxonomy-term/

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