Home › Forums › ACF PRO › Return Taxonomy Slug using Taxonomy Field › Reply To: Return Taxonomy Slug using Taxonomy Field
basically, if you want to get all of the posts in a term then you should use WP_Query https://codex.wordpress.org/Class_Reference/WP_Query and the documentation will lead you there https://developer.wordpress.org/reference/functions/query_posts/
The important bit is the tax_query and the fact that your field is returning an term object. You could query by any of the fields of the returned object.
If you want to see what the object looks like
$department = get_field('department');
echo '<pre>'; print_r($depearment'); echo '</pre>';
Here is
<?php
$department = get_field('department');
$query = array(
'post_type' => 'staff',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'departments',
'field' => 'term_id', // not needed, default value
'operator' => 'IN', // not needed, default value
'terms' => array($department->term_id)
)
)
);
$team_query = WP_Query($args);
if ($team_query->have_posts()) {
while($team_query->have_posts()) {
$team_query->the_post();
// output stuff about this post
}
wp_reset_postdata();
}
more information about querying based on different types of acf fields can be fount here https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
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.