Home Forums Backend Issues (wp-admin) set wp_title(); as acf field


set wp_title(); as acf field

  • Hi there,
    Is it possible to automatically set the page title “wp_title();” as one of my field values? So for example: When my page title is “Sports Arena” I would like to have my field called “the_field(‘arena’); set to “Sports Arena”.

    I tried the following:

    function update_acf_title($post_id){
    $post_title = get_the_title( $post_id );
    update_field('titel', $post_title, $post_id);

    Thanks a lot! Any help is appreciated,

  • Try setting your filter priority > 10

  • Hi John! Thanks for the advice. I editet the script like below but it still does not work:

    function update_acf_title($value,$post_id, $field){
    $post_title = get_the_title( $post_id );
    update_field('titel', $post_title, $post_id);
    return $post_title;

    I installed the ACF to Rest-API plugin to get all my fields via JSON but unfortunately I don’t have the page title, which is the most important thing 😉

    If you have any others ideas please let me know.

  • i cannot give you any advice on REST with ACF. I also am not sure about setting an ACF field based on the post title, most people want to do it the other way around… set a post title based on an ACF field. I’m not sure of the timing here. I would probably do this using an acf/save_post hook instead of an update_value hook.

  • Hi John, thanks for the advice. I edited my functions.php like below:

    function my_acf_save_post( $post_id ) {
    $post_title = get_the_title( $post_id );
    update_field('titel_acf', $post_title, $post_id);
    return $post_title;
    add_action('acf/save_post', 'my_acf_save_post', 20);

    The field titel_acf still does not get updated 🙁

  • what is the name of your field? titel or titel_acf, you’ve used both.

    If this field is just a basic text field and it’s not nested in any way then I don’t see any reason it’s not working as long as your filter is running.

  • Hi John,
    I managed to get it working. Thanks! Is there any possibility to save the title when I use bulk edit? The title is only saved when I open the custom post type and hit the save button. It is not working when I use quick edit or bulk edit.

  • ACFs functions and the hooks it calls are don’t run when these things save posts. You’d need to figure out what hook to use and then create additional filters or actions to do the work in these cases.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘set wp_title(); as acf field’ is closed to new replies.