Home › Forums › General Issues › priority issue with function
guys!
I have the following code:
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$posttypes = array( 'post', 'portfolio', 'recensies', 'page' );
$currentposttype = get_post_type();
if ( in_array( $currentposttype, $posttypes ) ) { //only run if is certain post-type
if( $currentposttype == 'post' || $currentposttype === 'portfolio' || $currentposttype === 'page' ) {
$my_post['post_title'] = get_field('kop');
} elseif( $currentposttype === 'recensies') {
$my_post['post_title'] = get_field('persoon') . ' (' . get_field('bedrijf') . ')';
}
//Unhook function to prevent infitnite looping
remove_filter('acf/save_post', 'my_post_title_updater', 20);
// Update the post into the database
wp_update_post( $my_post );
//Rehook function to prevent infitnite looping
add_filter('acf/save_post', 'my_post_title_updater', 20);
}
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
this is a function to set the post_title
of various post types with ACF fielddata.
my issue:
the ACF tax field does not save the tax to the wordpress post itself, this is because of the priority 20
in remove_filter('acf/save_post', 'my_post_title_updater', 20);
according to @Elliot:
My thinking was that your update_post was triggering another save within the ACF plugin, this would then cause the taxonomy field to attempt to save again, however, due to some logic, the terms would be lost.
By changing the priority to before ACF starts to save, the terms will exist correctly and the logic will work
So great you think, issue solved. not yet unfortunately. when I set it to 1
: remove_filter('acf/save_post', 'my_post_title_updater', 1);
my taxonomy saves, but I get an infinite loop with posttype page
and post
, because according to @acf-support (James) they need equal priorities, which I can confirm is true.
right now I have to choose either one 1
or 20
so I have a fucntion thats not fully working, how can I solve this?
Thanks guys!
You’re add_action and remove filter need to have the same priority.
add_action('acf/save_post', 'my_post_title_updater', 1);
remove_filter('acf/save_post', 'my_post_title_updater', 1);
When you run the acf/save_post action with a priority of 1 you will need to use the $_POST['acf']
array to get the values of the other fields.
Thanks for the reply @hube2
I lost you at this part:
When you run the
acf/save_post
action with a priority of 1 you will need to use the$_POST['acf']
array to get the values of the other fields.
can you help me out with that?
When you use a priority of <10 for the acf/save_post action it will run before ACF has updated the values of the fields. Using get_field() will return the old value of the field instead of the new value, so you can’t use get_field() if your update depends on the new values.
Instead you need to get the values from the $_POST values. This is an array of posted values. ACF using the ‘acf’ index of this for all field values and the index for each field is the field key for the field.
// example of $_POST
$_POST['acf'] = array(
'field_0123456789acb' => 'value of field here',
'field_123456789abcd' => 'value of field here',
// etc.
);
Thanks @hube2! I completely understand, I changed my code to the following:
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$posttypes = array( 'post', 'portfolio', 'recensies', 'page' );
$currentposttype = get_post_type();
if ( in_array( $currentposttype, $posttypes ) ) { //only run if is certain post-type
if( $currentposttype == 'post' || $currentposttype === 'portfolio' || $currentposttype === 'page' ) {
$my_post['post_title'] = $_POST['acf']['field_56df22028f627'];
} elseif( $currentposttype === 'recensies') {
$my_post['post_title'] = $_POST['acf']['field_56dee620f84d5'] . ' (' . $_POST['acf']['field_56dee62cf84d6'] . ') - ' . $_POST['acf']['field_5734c3bcceb6c'];
}
//Unhook function to prevent infitnite looping
remove_filter('acf/save_post', 'my_post_title_updater', 1);
// Update the post into the database
wp_update_post( $my_post );
//Rehook function to prevent infitnite looping
add_filter('acf/save_post', 'my_post_title_updater', 1);
}
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 1);
now all my post_title
updaters work, but my custom taxonomies are still not saved to the post, what are we missing here?
I don’t know, it looks like you’re doing everything according to the other topic.
I found several other topics on this same type of thing but I can’t fine the one quote Elliot in your OP.
At this point I think you should open up a new support ticket https://support.advancedcustomfields.com/new-ticket/ they might be able to help you more than I am. I don’t see any reason why the terms should not be saved correctly.
Thanks John!
I will contact support.
regarding Elliot’s quote, I got it from a support mail he sent me.
Thanks for helping out!
It seems odd to me that the terms are being removed from a post when you change the post title. I’ve actually done this on several projects and I’ve never had a problem having it run on acf/save_post with a priority of 20. This makes me think that there may be other filters running on the site that are causing some type of interference.
Looking at your code again I have a one suggestion, but I’m not sure it’ll change anything.
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$posttypes = array( 'post', 'portfolio', 'recensies', 'page' );
// specify post id in get_post_type
$currentposttype = get_post_type($post_id);
if ( in_array( $currentposttype, $posttypes ) ) { //only run if is certain post-type
if( $currentposttype == 'post' || $currentposttype === 'portfolio' || $currentposttype === 'page' ) {
$my_post['post_title'] = $_POST['acf']['field_56df22028f627'];
} elseif( $currentposttype === 'recensies') {
$my_post['post_title'] = $_POST['acf']['field_56dee620f84d5'] . ' (' . $_POST['acf']['field_56dee62cf84d6'] . ') - ' . $_POST['acf']['field_5734c3bcceb6c'];
}
//Unhook function to prevent infitnite looping
remove_filter('acf/save_post', 'my_post_title_updater', 1);
// Update the post into the database
wp_update_post( $my_post );
//Rehook function to prevent infitnite looping
add_filter('acf/save_post', 'my_post_title_updater', 1);
}
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 1);
Thanks for your patience with this problem whilst we attempt to help.
The taxonomy field uses some different logic that others to ‘save’ the selected terms to the post. Because there may be multiple taxonomy fields saving terms (sub fields), this field type appends the selected values to an array, and then waits until later in the ‘acf/save_post’ action to save them all in 1 go.
The good news is this is all done by the priority of 15.
If we go back to your original code where the priority is 20, this is a good start.
By this time (20), ACF has already saved it’s data, so I wonder if you could temporarily remove the $_POST[‘acf’] data before you re-save the post.
This way, ACF won’t run any acf/save_post data, and will hopefully avoid any issues with re-saving the selected terms.
Can you try this:
<?php
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$posttypes = array( 'post', 'portfolio', 'recensies', 'page' );
$currentposttype = get_post_type();
if ( in_array( $currentposttype, $posttypes ) ) { //only run if is certain post-type
if( $currentposttype == 'post' || $currentposttype === 'portfolio' || $currentposttype === 'page' ) {
$my_post['post_title'] = get_field('kop');
} elseif( $currentposttype === 'recensies') {
$my_post['post_title'] = get_field('persoon') . ' (' . get_field('bedrijf') . ')';
}
// backup $_POST
$acf_post = acf_extract_var($_POST, 'acf');
// Update the post into the database
wp_update_post( $my_post );
// restore $_POST (most likely not even needed)
$_POST['acf'] = $acf_post;
}
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
?>
Hi @elliot!
Thanks for jumping in and elaborating what might go wrong, I really appreciate the help.
I tried this function, it does save ‘kop’ field and taxonomies in my custom post types, however, with the post
and page
types, it behaves very strange:
when creating a new one, it saves the kop
fine, when updating it after creation, it does not update the field, but when updated the second time, it uses the value of the first update….. strange right? you can say it’s always ‘one behind’
Do you need login credentials so you can see this yourself on my staging area?
Thanks!
Thanks for the reply.
I’ve just created a field named ‘kop’ and tested the exact above code.
I can’t replicate any issue, everything saves correctly and the post’s title is updated as expected.
I’m using ACF PRO 5.3.8.1, what version are you running?
hi @elliot
Im running the latest version as well.
I just tried the code on my staging site (not the local one) and it works! verified it on other installs as well.
thank you very much for jumping in!
I hope this function of mine will continue working now and won’t break again because of something
Thanks!
The topic ‘priority issue with function’ 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.