Home › Forums › Front-end Issues › Taxonomy related posts.
Hi, i want to show related posts in my custom post type with taxonomy automaticly.
i have a custom post type which “film” and they have custom taxonomies like genres “scary” “history” “drama” . When you enter a single-film.php i want in bottom some of 5 post which they also same taxonomies like “scary” or “history”
I know we can make it in wordpress panel but i dont want to do it manually. I want automatic loop for that.
There isn’t anything in ACF that will automatically add what you’re looking for to the front of the site. I don’t even know of any plugins that will do this for you. You’ll need to code the queries and loops. If you’re looking for a plugin that will manage the fields and create the template without coding there are other plugins that will do that. As a user I see ACF as a tool for coders.
Sine you’re a coder I won;y go into coding and I’ll just give you the outline.
The first thing you need to do is to get the terms for the current post The first thing you need to do is to get the terms for the current post.
Next you do a new WP_Query, including a tax_query to get other posts that are in the same term http://codex.wordpress.org/Class_Reference/WP_Query
and finally you loop through the returned posts, if any, to show the list.
Let me know if you need help any of the code that goes with these steps.
<?php
$selected = get_field('turler');
$args = array(
'post_type' => 'film',
'tax_query' => array(
array(
'taxonomy' => 'tur',
'field' => 'slug',
// the default operatre is "IN"
// there is no == operator for tax_query
// see https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
'terms' => array($selected),
),
),
);
$angebot_2 = new WP_Query( $args );
while( $angebot_2->have_posts() ) : $angebot_2->the_post();
?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?>
You asked what you’re problem was with the query you had. I commented the error that I saw. I’m not sure what you’re looking for.
if this returns the term in the taxonomy of the current post
$selected = get_field('turler');
then the query will display all of the posts in the same term.
The topic ‘Taxonomy related posts.’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.