Hey, John!
I’m trying to do this:
add_filter('acf/update_value/name=categories', 'set_default_category', 9, 3);
function set_default_category($value, $post_id, $field) {
if (!is_array($value) || !count($value)) {
$value = array(get_option('default_category', 1));
}
return $value;
}
I know it triggers, and I know $value
has the id of the selected default category.
Yet, when the the post is saved, the category doesn’t show up.
Any tips?
Thanks, John! I’ll probably work around it by using the filter in your example.
Yeah, the desired behavior is that when the customer doesn’t select a category, the default is applied. I noticed it didn’t happen, and I need it to happen because we hide posts based on categories on the site in the pre_get_posts action.
Thanks!
I fixed it now!
I changed the 5 lines to this:
add_filter('acf/update_value/type=relationship' ...
.
I also saw you used get_the_title()
, which is a better idea than what I was doing, which was get_post()
.
Now it works, and the values I need are saved in the database.
Also, since I have 5 different fields per post, I use the $field['_name']
plus a string to get seperate a meta_key
per relationship field per post.
Thanks, John!
This was similar to how I solved it yesterday 🙂
I got it almost working, but I need this on 5 relationship fields on the page.
I’ve tried to add the filter 1 time for each realtionship field, but then it seems like not all of them are run.
Maybe 5 are too many.
I tried this:
add_filter('acf/update_value/name= ...
add_filter('acf/update_value/name= ...
add_filter('acf/update_value/name= ...
add_filter('acf/update_value/name= ...
add_filter('acf/update_value/name= ...
Ah, I found my mistake. I did what you said, disabled the plugins and changed the theme. That worked out.
When I tried to changed it back to the theme we’re using, and put the code in functions.php, earlier it was in another file, it worked.
Then I put it in the original file which gets included in the functions.php file, and it stopped working. Then, I noticed I forgot to add the namespace before the callback.
So this works:
function my_relationship_query( $args, $field, $post_id ) {
// only show children of the current post being edited
$args['post_parent'] = 7;
// return
return $args;
}
// filter for every field
add_filter('acf/fields/relationship/query/name=_theme', __NAMESPACE__ . '\my_relationship_query', 10, 3);
I’ll change the id of the post to a field which they can set on an options page.
Sorry about that, and super thanks for your help!
Hope I didn’t take to much of your time 🙂
Hello, John!
I have version 5.4.3.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.