Custom Fields
Re-Envisioned
Support MB Views Display Recent Post from the Terms

  • Creator
    Topic
  • #28673
    Prabakaran Shankar
    Participant

    Hi,

    I'm very much a fan of the powerful MB Views along with custom fields, relationships and queries. I'm trying to display a recent post from a specific term. I have gone through the documentation showing related posts using MB Relationships.

    If there is a possibility to show the post, please help me.

    Thanks in advance.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Author
    Replies
  • #28684
    Long Nguyen
    Moderator
    #28711
    Prabakaran Shankar
    Participant

    Thank you very much for your suggestion.

    According to the discussion in the two references, they were used in archive pages. If I am mistaken, please let me know.
    Anyhow, I have tried the following code to display recent posts in the category.

    `<div class=box><p>Related posts</p>
    {% set args = { post_type: 'job', posts_per_page: 2, tax_query: [ { taxonomy: 'field'} ] } %}
    {% set job = mb.get_posts( args ) %}
    {% for post in job %}
    {{ post.post_title }}<br>
    {% endfor %}
    </div>
    // CPT - Job; Custom taxonomy - Field;
    I have used this code in post singular to display after the post content

    #28712
    Prabakaran Shankar
    Participant

    Hello,
    without the tax_query, the code displays the recent post title. (if possible please share the term to be used to display post title with URL instead of {{post.post_title}})

    in case, if tax_query with the dynamic taxonomy terms like the below, it shows error!
    '{% set args = { post_type: 'job', posts_per_page: 5, tax_query: [ { taxonomy: '[{{ mb.get_the_term_list( post.ID, 'job-fields', '', ', ' ) }}]'} ]} %}
    {% set my_posts = mb.get_posts( args ) %}
    {% for post in my_posts %}
    {{ post.post_title }}<br>
    {% endfor %}

    Please help me!

    #28723
    Long Nguyen
    Moderator

    Hi,

    Follow on this topic https://metabox.io/support/topic/cpt-loop-by-category/, you can see the variable args

    {% set args = { post_type: 'project', posts_per_page: 10, tax_query: [ { taxonomy: 'taxonomy-slug', field: 'slug', terms: 'term-slug' } ] } %}

    For example, I have a post type slug book, taxonomy slug book-kind, term slug novel. Screenshot https://share.getcloudapp.com/6qurDGdB. The variable args should be

    {% set args = { post_type: 'book', posts_per_page: 10, tax_query: [ { taxonomy: 'book-kind', field: 'slug', terms: 'novel' } ] } %}

    #28821
    Prabakaran Shankar
    Participant

    Hi,

    Thank you for your suggestion.

    It is working.

    One more request!
    Is that possible to use helper function or mb. proxy to get term list automatically to use in tax_query?
    for example:

    {% 
    set args = { post_type: 'book', posts_per_page: 10, 
    tax_query: [ { 
    taxonomy: 'book-kind', 
    field: 'slug', 
    terms: '{% for item in post.job_category_field %}{{ item.name }}{% endfor %}' 
    } ] 
    } 
    %}

    or

    {% 
    set args = { post_type: 'book', posts_per_page: 10, 
    tax_query: [ { 
    taxonomy: 'book-kind', 
    field: 'slug', 
    terms: '{{ mb.get_the_term_list( post.ID, 'job-field' ) }}' 
    } ] 
    } 
    %}
    #28835
    Long Nguyen
    Moderator

    Hi,

    What is the Type and Location of your View? On the archive page or single post page? Please share some screenshots when editing the View.

    #28906
    Prabakaran Shankar
    Participant

    Hello,

    from the contiunation.
    I have used the below query to display CPT Post ID, post title and URL. It displays post ID as per query, but there is no post URL and POst title.

    {% set args = { post_type: 'job', posts_per_page: 10, tax_query: [ { taxonomy: 'category', field: 'slug', terms: 'ongoing' } ] } %}
         {% set posts = mb.get_posts( args ) %}
        {% for post in posts %}
        <li>{{ post.url }}, {{ post.ID }}, {{ post.title }}</li>
        {% endfor %}

    Results are:

        <li> 230, , </li>
            <li> 225, , </li>

    Please help me with the mistake in the above code.

    #28907
    Prabakaran Shankar
    Participant

    In the single post page as well in the sidebar to show the category-based recent post.

    #28917
    Long Nguyen
    Moderator

    Hi,

    Please follow this documentation to know how to use the custom query https://docs.metabox.io/extensions/mb-views/#custom-query

    To get the post URL in the custom query, you can use the WordPress function get_the_permalink().

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