Home › Forums › Bug Reports › get_field() from WP_Term returns ID on archive page
Hello!
I’m trying to access a custom image field on categories. On regular pages, it works perfectly.
I simply do:
$cat_args = array(
'taxonomy' => 'category',
'exclude' => 1,
'hide_empty' => true
);
$categories = get_terms( $cat_args );
foreach ($categories as $category) :
$poster = get_field('category_bg', $category);
echo $poster ? '<img class="visuallyhidden" src="' . $poster['sizes']['big'] . '" alt="" />' : '';
endforeach;
on the pages, $poster is an object while on the archive page, $poster is a string containing ID :/
More precisions : it’s a custom post archive. Custom Post Type : oeuvre
Thanks π
90% of the time when this happens it is due to a conflict with something. It could be a another plugin in something in your theme. More often then not it has been a pre_get_posts filter that is interfering with the acf queries.
You need to try to narrow down what it is by deactivating plugins and maybe switching themes.
If you do a search in this forum for “image field returns id instead of array” the topics that come up may help you.
Hello @hube2 !
Thank you for your message !
You were right, it was a conflict with a filter function that i’ve added to show Custom Post Types in archives. Otherwise, only posts are displayed :/
This one exactly:
/**
* Archives.php only shows content of type 'post', but you can alter it to include custom post types.
* Add this filter to your functions.php file
* @url https://css-tricks.com/snippets/wordpress/make-archives-php-include-custom-post-types/
*/
function proov_add_custom_types( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'post', 'nav_menu_item', 'oeuvre'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'proov_add_custom_types' );
I’m gonna try something else ^^
function proov_add_custom_types( $query ) {
// add condition to make sure it's the the main query
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] )
&& $query->is_main_query() ) {
$query->set( 'post_type', array(
'post', 'nav_menu_item', 'oeuvre'
));
return $query;
}
}
add_filter( 'pre_get_posts', 'proov_add_custom_types' );
I found a solution on the Codex π
This works π
/**
* Registering Post Types
*/
// Show posts of 'post', 'page' and 'oeuvre' post types on category page
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_category() && $query->is_main_query() )
$query->set( 'post_type', array(
'post',
'page',
'oeuvre'
));
return $query;
}
You must be logged in to reply to this topic.
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!
βEver wondered when and why ACF uses JSON instead of the database? Check out our summary of the most recent session of ACF Chat Friday for the answer, and make sure to register for the next session.
— Advanced Custom Fields (@wp_acf) February 23, 2023
π https://t.co/3UtvQbDwNmhttps://t.co/wfFEVcXVKc
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.