Display Recent Post from the Terms
- This topic has 10 replies, 2 voices, and was last updated 3 years, 5 months ago by
Prabakaran Shankar.
-
AuthorPosts
-
June 3, 2021 at 1:20 PM #28673
Prabakaran Shankar
ParticipantHi,
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.
June 3, 2021 at 9:56 PM #28684Long Nguyen
ModeratorHi,
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/June 4, 2021 at 11:38 PM #28711Prabakaran Shankar
ParticipantThank 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 contentJune 5, 2021 at 12:36 AM #28712Prabakaran Shankar
ParticipantHello,
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!
June 5, 2021 at 1:40 PM #28723Long Nguyen
ModeratorHi,
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 slugbook-kind
, term slugnovel
. 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' } ] } %}
June 10, 2021 at 3:27 PM #28821Prabakaran Shankar
ParticipantHi,
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' ) }}' } ] } %}
June 10, 2021 at 9:03 PM #28835Long Nguyen
ModeratorHi,
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.
June 15, 2021 at 2:49 PM #28906Prabakaran Shankar
ParticipantHello,
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.
June 15, 2021 at 2:52 PM #28907Prabakaran Shankar
ParticipantIn the single post page as well in the sidebar to show the category-based recent post.
June 15, 2021 at 11:34 PM #28917Long Nguyen
ModeratorHi,
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().
November 6, 2021 at 2:49 AM #31765Prabakaran Shankar
ParticipantPlease 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.
-
AuthorPosts
- You must be logged in to reply to this topic.