Display Recent Post from the Terms

Support MB Views Display Recent Post from the TermsResolved

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #28673
    Prabakaran ShankarPrabakaran 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.

    #28684
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Please follow related topics to show posts from a specific term
    https://support.metabox.io/topic/cpt-loop-by-category/
    https://support.metabox.io/topic/displaying-two-cpts-in-custom-taxonomy-term/

    #28711
    Prabakaran ShankarPrabakaran 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 ShankarPrabakaran 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 NguyenLong Nguyen
    Moderator

    Hi,

    Follow on this topic https://support.metabox.io/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 ShankarPrabakaran 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 NguyenLong 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 ShankarPrabakaran 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 ShankarPrabakaran Shankar
    Participant

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

    #28917
    Long NguyenLong 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().

    #31765
    Prabakaran ShankarPrabakaran Shankar
    Participant

    Please close this topic. I have found an alternative way using PHP to display the recent post based on the tags.

    Thank you very much for your valuable time. Really appreciate your kind support to make it happen.

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