Display Related Taxonomies
- This topic has 6 replies, 2 voices, and was last updated 2 years, 9 months ago by
Marius Doko.
-
AuthorPosts
-
July 4, 2022 at 1:22 AM #36780
Marius Doko
ParticipantHello, I have a taxonomy Profiles which is displayed in 2 CPTs, Agent and News.
I am trying to create an author bio box that will be showing related Profiles at the end of the content. In the backend, I have checked which Agent will be displayed but instead, it is showing all agents which are in Taxonomy. Please could help me with this issue?
Below is my code in MB View{% set args = {taxonomy: 'profile',hide_empty: false} %} {% set profiles = mb.get_terms( args ) %} <section class="author-box container"> <div class="panel panel-default"> <div class="panel-body"> <div class="row"> {% for profile in profiles %} <div class="col-xs-6 col-xs-offset-3 col-sm-3 col-sm-offset-0"> <img src="{{ profile.agent_image.medium.url }}" /> </div><!-- col-sm-3 --> <div class="col-xs-12 col-sm-9"> <hr class="visible-xs-block"> <div class="author-box-intro small text-muted">About the Author</div> <!-- .author-box-intro --><div class="author-inline-block"> <h4 class="author-box-title"> <span class="author-name text-primary">{{ profile.name }}</span><small class="author-social-links small"><a href="#" target="_blank" rel="noopener"><i class="fa fa-2x fa-twitter"></i></a><a href="#" target="_blank" rel="noopener"><i class="fa fa-2x fa-google-plus"></i></a> </small><!-- author-social-links --> </h4> </div> <div class="author-box-content">{{ profile.content }} </div><!-- author-box-content --> </div><!-- col --> {% endfor %} </div><!-- row --> </div><!-- panel-body --> </div><!-- panel --> </section><!-- author-box -->
July 4, 2022 at 12:45 PM #36783Long Nguyen
ModeratorHi Marius,
Do you mean to display an author box at the bottom of the single post Agent? You can use the argument
object_ids
to get all terms associated with the current post.{% set args = { taxonomy: 'profile', hide_empty: false, object_ids: post.ID } %}
Read more on the documentation https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/
July 4, 2022 at 4:02 PM #36785Marius Doko
ParticipantHey Long,
Thank you for your reply.It worked. One additional question-
I have created the association between News(Article) and Agent(Profile). The agent is being displayed for one Article. But now in Agent CPT where this Agent has the name associated with her profile, I would like to display all Articles that are written by her. Should I do again the association between Single post Agent and Single Article? I thought that this is going to be automatically added for that agent since i have created the association between That article with an agent in News CPT. Or I am doing anything wrong that isn't working that wayThank you
July 5, 2022 at 6:22 AM #36791Long Nguyen
ModeratorHi Marius,
If you want to display all posts (News) with the same taxonomy associated (Profile), please use the function
get_posts()
with the parametertax_query
Read more on the documentation https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
https://developer.wordpress.org/reference/functions/get_posts/July 7, 2022 at 10:02 PM #36863Marius Doko
ParticipantHello Long,
Thank you for your suggestion, I was looking for a solution for this case but i dont know if i am doing any mistake in Twig syntax or i have any mistake in the CPT - Taxonomy configuration. If you could help me with this case i would be really happy since i am new to using twig.
I have a CPT "Review" - Contains 2 Taxonomies, Profile ( which is used for Agent CPT) and Location (which is used for Location CPT). Also other way, these 2 taxonomies have that CPT on their configuration.
What i am trying to do is:
I have created a test review and i have checked for which Profile taxonomy is that review.At Agent CPT (i have displayed all the fields from Profile taxonomy) since 1 agent have 1 taxonomy on its own. Now i would like to add a section where i can display all the related Reviews for that agent.
The code is as below{% set args = { post_type: 'review', tax_query: [{ taxonomy: 'profile', field: 'slug'}] } %} {% set reviews = mb.get_posts( args ) %} {% for review in reviews %} {{ review.post.title }} {{ review.post.content }} {{ review.post.content }} {{ review.post.rating }} {% endfor %}
I have tried also another way:
{% set reviews = mb.wp_get_post_terms { post_type: 'review', 'profile' } %} {% for review in reviews %} {% set args = { post_type: 'review', tax_query: [{ taxonomy: 'profile', field: 'id', terms: review.term.id }] } %} {{ review.post.title }} {{ review.post.content }} {{ review.post.content }} {{ review.post.rating }} {% endfor %}
Please could you help me with a solution for this case ?
Thank you 🙂July 8, 2022 at 11:04 PM #36881Long Nguyen
ModeratorHi Marius,
Here is the guide for this case
- Get terms associated with current Agent CPT
- Use the loop to iterate through the term array
- Get post typereview
based on the term ID (tax_query)
- Use the loop (nested) to iterate through the post arraySomething like
{% set profile_terms = mb.wp_get_post_terms ( post.ID, 'profile' ) %} {% for profile_term in profile_terms %} {% set args = { post_type: 'review', tax_query: [{ taxonomy: 'profile', field: 'id', terms: profile_term.term_id }] } %} {% set reviews = mb.get_posts( args ) %} {% for review in reviews %} {{ review.post_title }} {% endfor %} {% endfor %}
July 9, 2022 at 5:27 AM #36887Marius Doko
ParticipantThanks a lot Long, was a great help! consider this ticket to be completed 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.