So, the issue I have is that get_field_object() returns an empty value when creating a new post.
Also, none of these solutions work correctly if you have the same slug in multiple field collections for different post types.
Really wish ACF has a get_field_info( $post_type, $post_slug );
Just got the beta to try myself…
It seems to me that multiple update_field()
calls is very inefficient and I did not see an update_fields()
call in the docs.
Okay, reading through other posts here I see that doing a query against repeater fields is discouraged (and difficult).
So, my thought is to serialize the data I want to query and add it as a separate custom-field during “save_post”.
So apparently part of my answer is here, in the old Codex:
https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
Just need to figure out how to determine WHICH cpt is being edited.
function my_enqueue($hook) {
if ( 'edit.php' != $hook ) {
return;
}
wp_enqueue_script( 'my_custom_script', get_template_directory_uri() . '/myscript.js' );
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );
Actually, that begs a second question…
I use some code to put ACF forms on the front side of the web site. So, which hook/filter do I use to load my JavaScript file and can I test to see if the form is on the front end or back end?
That Query Monitor plugin was just the trick!
Thanks very much. I just like flow charts and to know how things work and in what order.
I am working on a fairly large and complex project and this helps me to visualize the things I need to do and when they get done.
This is my guess on order when creating/editing a post:
The actions/filters are not listed in the docs in the order they fire and some of the descriptions are ambiguous.
For example, does “acf/init” fire after everything is done? All fields are rendered? Is it literally the last action to fire?
Thanks!!
What type of field is it? What is its value?
Never mind… It was a 3rd party addon.
When I have questions like this I generally create a field like the type I want to use for my field with the settings I want it to have and then do an export to code to see that the settings are.
Never though of that… Thanks!!
What about using 'form' => false
in your call to acf_form so that ACF renders the fields, but doesn’t process them?
How do you specify a default value for “acf/render_field_settings”?
I have a “true_false” field type and it always defaults to true… I want it to default to false.
Why isn’t this a feature of ACF?
ps. The plugin works, thanks!
This article was posted on Facebook and fixed my problem:
Okay, I added a check for wp_doing_ajax() and I no longer get “Loading error…”. I now get “No matches found” when logged in as a user. It works fine when I’m logged in as an admin.
Here is my code:
add_action( 'init', 'am_block_user_from_admin' );
function am_block_user_from_admin() {
if ( wp_doing_ajax() )
exit;
if ( is_admin() && ! current_user_can( 'administrator' ) ) {
wp_redirect( home_url() );
exit;
}
}
Okay thanks, I submitted a ticket for a %post_id% feature request.
Never mind, changing the whole thing over to use acf/save with a priority of 20 fixed my problem.
Interesting…
Here’s a question. If I call wp_insert_post() to create a new post and I use the ACF field names in the meta_input array, will that trigger ACF to do it’s thing?
I didn’t find it in the codex. I caught a post on a blog about caching issues when using a third-party cache plugin.
In plain-vanilla WordPress just calling update_field() after wp_insert_post works perfectly. However, we are using WP Super Cache and I’ve seen update_field() fail because the post ID doesn’t exist in the database due to the data being cached in memory.
I don’t have the article on my work computer, I was working on my laptop over the weekend and it’s there.
In any case, WP Super Cache has a filter for database reads and writes and hooking that will flush the memory cache.
Code works fine now.
I found an article on flushing the WP cache to write the post_data to the DB so that I can call the ACF functions.
The problem with using update_field() is that according to the WordPress Codex, calls to update post meta after wp_insert_post() may fail if the post is still in memory (cached).
I did try that and it worked most of the time, but occasionally the post meta was not updated.
An “acf_insert_post()” function would be fantastic. Or a function that fixes the meta_input() array by adding the appropriate ACF info.
It looks like I might be able to construct something using the “get_sub_field_object()” function and passing the field name.
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.