Home › Forums › General Issues › Custom Post Type with a custom category
Hi from Germany,
I have a custom post type with a custom category. I added a custom location rule for a field group that works quite well. Here’s my matching function:
public function acf_location_rules_match_portfolio_category($match, $rule, $options){
// validate
if(!$options['post_id']){return false;}
// vars
$terms = get_terms( array(
'taxonomy' => 'portfolio_category',
'hide_empty' => false,
) );
$cats_get = get_the_terms($options['post_id'], 'portfolio_category');
$cats = array();
foreach($cats_get as $portfolio_category){
$cats[] = $portfolio_category->term_id;
}
if($rule['operator'] == "=="){
$match = in_array( $rule['value'], $cats);
} else if($rule['operator'] == "!="){
$match = !in_array( $rule['value'], $cats);
}
return $match;
}
Obviously, this only works after the post was saved, otherwise I could not get the custom taxonomy. But what I want to achieve is to show/hide this field group when the appropriate checkbox is checked or unchecked, just like it works with normal post categories.
Could anybody lead me to the right direction? Every bit of help is gladly appreciated.
I’m assuming that you’re using ACF4 because this is built into ACF5
open the file in your plugins
advanced-custom-fields/core/controllers/location.php
Look for the function/method rule_match_post_category()
Everything you need is there, you’ve basically just got to swap out post_category
for your custom taxonomy.
No, I indeed use ACF 5, Version 5.4.6 to be exact. There is no controllers golder in the core folder.
To illustrate my problem better, I took four screenshots. The first one shows the rules that I use for the field group:
Next one is the add new portfolio screen. See the checked portfolio category CD Covers? The red arrow points to the missing meta box.
The third screenshot ist after editing a saved portfolio entry. Again notice the checked portfolio category.
The last screenshot shows the same saved portfolio after I unchecked the portfolio category. The Metabox remains in place.
Of course there aren’t any JS errors showing up in my Firebug Console. I hope the screenshots could explain my problem. MAybe I am simply too stupid, or I ran into a little issue.
Any Idea about that?
Well, it seems, the images aren’t coming through. So here are the direct links to the uploaded images:
Screen 1: https://postimg.org/image/mu19froax/
Screen 2: https://postimg.org/image/nktzljqo9/
Screen 3: https://postimg.org/image/zb7x2xjgp/
Screen 4: https://postimg.org/image/gulgge2gz/
Sorry, I can’t read the language so I can’t say, the images don’t help me much
From what I understand reading you want to add a field group to a posts with a specific term of a custom taxonomy. This is what it appears you are trying to do by looking at the images. This is built into ACF5.
"Post Taxonomy" "is equal to" "My Custom Taxonomy Term"
If you want to do this yourself, ACF passes the values of the terms selected in $options[‘post_taxonomy’], you can see an example of where I get this value and determine what terms are selected on line 26 of this file: https://github.com/Hube2/acf-filters-and-functions/blob/master/acf-post-category-ancestor-location-rule.php
But I could be misunderstanding what you want to do and here are some others
If you want to add it to the term edit page then you’d use
"Taxonomy Term" "is equal to" "My Custom Taxonomy"
I you are trying to add the field group to a specific term edit page of the custom taxonomy, they you could be right. In order to make is work when you select it instead of saving the post. ACF passes values to your location match filter in the $options argument.
The best source of information for creating custom location rule is, like I said, to look at the ones that are built into ACF. These can be found in
/wp-content/plugins/advanced-custom-fields-pro/core/location.php
All of the custom locations rules I’ve eve built have started by copying some portion of the code from one of the existing location rules or by using these as examples to build my own code.
You can make the location rules dynamic by checking for the values in other arguments that are passed to your location match rule. You can see all the values that are passed to your rule starting on line 1237 of the file I mentioned.
Thank you for your patience, John. Sometimes one is simply too blind to see. I tried to replicate a function that is allready there. So all my matching stuff became obsolete at once. Thanks again for your help.
Did I ever mention that ACF became absolutely indespensable for me? I use it in every single project I work on.
Not a problems, sometimes it’s easy to overlook things.
I have not built a site in the last 4 or 5 years, or maybe longer, that did not use ACF. Honestly, I can’t remember ever not using it.
The topic ‘Custom Post Type with a custom category’ 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.