I have the following ACF set up:
1. Taxonomy field
http://cl.ly/1e1Z0G3F1n1V
http://cl.ly/3M3G3O3P0E46
2. WP_Query loop for CPT within WP template
$client_args = array (
'post_type' => array( 'clients' ),
);
$query_clients = new WP_Query( $client_args );
if ( $query_clients->have_posts() ) {
$terms = wp_get_object_terms( $post->ID, 'key_tasks' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo '<p>' .$term->name. '</p>';
}
}
echo '<pre>';
var_dump( $terms );
echo '</pre>’;
} else {
// no posts found
}
wp_reset_postdata();
3. Output is “array (size=0)”
The wp_get_object_terms block I’ve used works within a xxxx-single.php page, but not within this custom loop within a page template.
Any ideas?
Thanks
Hi @juxprose
I believe you need to use the_post() function to get make it work. Could you please try the following code:
$client_args = array (
'post_type' => array( 'clients' ),
);
$query_clients = new WP_Query( $client_args );
if ( $query_clients->have_posts() ) {
while( $query_clients->have_posts() ) { $query_clients->the_post();
$terms = wp_get_object_terms( $post->ID, 'key_tasks' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo '<p>' .$term->name. '</p>';
}
}
echo '<pre>';
var_dump( $terms );
echo '</pre>';
}
} else {
// no posts found
}
wp_reset_postdata();
This page should give you more idea about it: https://codex.wordpress.org/Class_Reference/WP_Query.
I hope this helps 🙂
Thanks James.
I’m afraid I omitted the while loop from my original example code, I am in fact using the same as yours – this:
while( $query_clients->have_posts() ) { $query_clients->the_post();
Here’s my full code:
$client_args = array (
'post_type' => array( 'clients' ),
);
$query_clients = new WP_Query( $client_args );
if ( $query_clients->have_posts() ) {
while ( $query_clients->have_posts() ) {
$query_clients->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php
// Key Tasks
// --------------------
$terms = wp_get_object_terms( $post->ID, 'key_tasks' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo '<p class="item-text">' .$term->name. '</p>';
}
}
echo '<pre>';
var_dump( $terms );
echo '</pre>';
<?php } ?>
<?php } else {
// no posts found
}
wp_reset_postdata();
Hi @juxprose
That’s weird. Your setup should save the selected terms to your posts unless if you set the “Save Terms” and “Load Terms” after you saved your posts. Could you please check the custom post editor on the backend if the terms are selected in the meta box (the one on the sidebar that comes with WordPress, not the one that is created by ACF)?
If you want, you can also get the terms by using the get_field() function. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/taxonomy/.
Thanks 🙂
Thanks James
To confirm, the terms are being saved, see here:
When using this example – https://www.advancedcustomfields.com/resources/taxonomy/ – like so:
<?php
$terms = get_field('key_tasks_list');
if( $terms ): ?>
<ul>
<?php foreach( $terms as $term ): ?>
<h2><?php echo $term->name; ?></h2>
<?php
echo '<pre>';
var_dump( $terms );
echo '</pre>';
endforeach; ?>
</ul>
<?php endif; ?>
I get NULL returned.
Thanks
Hi @juxprose,
Thanks a lot for the feedback.
Kindly share with us an export of the field so that I may test it on my end and try to help.
Hope to hear from you soon.
Thanks for the continued explorations into this, it’s much appreciated. Field group export attached.
Thanks.
Hi @juxprose
Because the terms are saved correctly on the backend, the issue with the first method is related to the wp_get_object_terms()
function. Could you please make sure that you use the right slug? For further support, could you please ask it in WordPress support forum?
Regarding the get_field()
issue, could you pass the post ID as a second parameter of the function like the following?
$terms = get_field('key_tasks_list', 99);
Where ’99’ is the ID of the post.
If the issue persists with the get_field() function, could you please open a new ticket here: https://support.advancedcustomfields.com/new-ticket? Please provide temporary admin credentials to your site in that ticket and the link to this thread.
Thanks!
Thanks for all your help James, I got it working using get_the_term_list
, like so:
$terms = get_the_term_list( $post->ID, 'key_tasks', '', ',' ); $terms = strip_tags( $terms );
if ($terms) {
echo '<p>'.$terms.'</p>';
} else {
}
The topic ‘Get taxonomy terms from custom loop’ 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.