Home › Forums › General Issues › post title updater not working for pages
Hi guys!
I have this function to update the post title of various post types:
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$posttypes = array( 'page', 'post', 'portfolio' );
$currentposttype = get_post_type();
if ( in_array( $currentposttype, $posttypes ) ) {
$my_post['post_title'] = get_field('kop');
}
//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', 20, 3);
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
I ‘s working perfectly for post
and portfolio
but fails with page
why is this? they’re all a post type right? Did debugging with var_dump
and found nothing….
Thanks for helping!
Please make sure that you set the id for the second parameter of the get_field() function. Could you please try this code:
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$posttypes = array( 'page', 'post', 'portfolio' );
$currentposttype = get_post_type($post_id);
if ( in_array( $currentposttype, $posttypes ) ) {
$my_post['post_title'] = get_field('kop', $post_id);
}
//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', 20, 3);
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
Let me know how it goes 🙂
Hi James! Thanks for the help!
it’s still not working though! neither for post
neither for page
only for cpt’s…..
Im getting a little lost here, using a conditional statement (just to check):
if ( $currentposttype == 'portfolio' ) {
$my_post['post_title'] = get_field('kop', $post_id);
}
still seems to fire the updater when page or post and gets me the infinite loading with a No data received ERR_EMPTY_RESPONSE
error in Chrome….
You need to set the same priority for the unhook and rehook function. It should be something like this:
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$posttypes = array( 'page', 'post', 'portfolio' );
$currentposttype = get_post_type($post_id);
if ( in_array( $currentposttype, $posttypes ) ) {
$my_post['post_title'] = get_field('test_custom_field_text', $post_id);
}
//Unhook function to prevent infitnite looping
remove_action('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);
Hope this helps 🙂
Thanks buddy! sorry for the late reply, but the priority solved the issue! very happy, thanks!
Hi, Im using the above, and it works for me,
What I added, in order to make the post slug work is this:
While it works, I am not sure whether it is the best approach, seems to make the saving of a post very slow.
add_action('save_post', 'set_slug');
function set_slug($post_id){
$new_slug = get_post_meta($post_id,'custom-slug', true);
$post_args = array(
'ID' => $post_id,
'post_name' => $new_slug,
);
wp_update_post($post_args);
}
I realise that my problem in saving the slug, is that it causes an infinite loop.
Instead, I added
$my_post['post_name'] = '';
to
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_name'] = '';
$posttypes = array( 'page', 'post', 'portfolio' );
$currentposttype = get_post_type($post_id);
The topic ‘post title updater not working for pages’ 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.