Home › Forums › Front-end Issues › ACF – Can't get custom post type by taxonomy, no idea how to fix
Although I’ve read many other answers with the same question on StackOverflow none of them where able to fix the issue I have.
I’m using ACF and have a custom post type called “services”. The CPT has a custom taxonomy called “category”, this returns the TermID of the taxonomy.
This was working before when I used a standard taxonomy, but since i’ve switched to the ACF taxonomy it has stopped working.
//This works fine
$serviceName = $wp_query->query_vars['test'];
//CURRENT The loop issue with ACF which doesnt work
$args = array(
'post_type' => 'services',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $serviceName
)
)
);
//OLD The loop when using standard taxonomy that works
$args = array(
'post_type' => 'services',
'category_name' => $serviceName,
);
//Display
$service_posts = get_posts( $args );
In Functions.php I have created a function which makes the ACF categories filter out the ones I don’t want. This is to prevent the user choosing categories which aren’t services.
function service_categories( $args, $field, $post_id ) {
$args['orderby'] = 'count';
$args['order'] = 'DESC';
$args['include'] = '6, 7, 8, 9, 10, 15, 16';
return $args;
}
Thank you for your time.
Also noticed when you view all the CPT’s the category tab is blank in the WP dashboard, not sure why this is since I assumed this overwrote the default wordpress taxonomy categories.
—
Ok so after investigating, it seems to me that the ACF taxonomy is not treated in the same way I thought the taxonomy would be treated, is the way I’m trying to do this possible or do I just have to use a custom radio button thats not even a taxonomy at all?
Do you have the taxonomy field set to save and load terms? If you do not do this then you need to change from a tax_query to a meta_query.
Still not working, felt like ive tried every variation.
The backend has modified slightly, but I am still using the ACF field.
Tried both variations nothing has worked.
$args = array(
‘post_type’ => ‘service_packages’,
// ‘meta_query’ => ‘service_category’,
// ‘meta_value’ => ‘5’
‘tax_query’ => array(
array(
‘taxonomy’ => ‘service_category’,
‘terms’ => ‘5’
),
)
);
5 is the id of the category.
Do you have the taxonomy field set to save and load terms?
Is the taxonomy field a top level field or a sub field of a repeater, group or flex field?
This worked in the end, thanks for the help! however not sure if this is exactly what I wanted, since the custom taxonomy isnt being treated as the default wordpress category, is there a way to make this happen? I guess it doesn’t have to really but the issue I have is with the permalink for each post.
Basically I have two custom posts.
A Service post type (it support,cloud, digital etc…)
A service package (this appears on each service page, they DO NOT have the own unique pages)
Basically the URL’s for the service packages should be.
services/{THE_CURRENT_TAXONOMY_FIELD_VALUE}/#post_name
But they are currently.
service/post_name
I tried using the overwrite rule, but obviously if I overwrite the slug so it contains the other post type, that post type will just become broken.
Thanks for the help!
EDIT:
I’ve realized i’m so stupid, I didn’t know that CPT allowed you to create custom taxonomies, i’ve wasted so much time on this. I’m very sorry.
The topic ‘ACF – Can't get custom post type by taxonomy, no idea how to fix’ 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.