Generate Post Title and Slug from Advanced Custom Fields


Advanced Custom Fields - Post Title

I often need to create post titles in WordPress based on fields I’ve generated using Advanced Custom Fields. Below is an example that assumes we have a Custom Post Type registered to “staff” and two Custom Fields with the name of “first_name” and “last_name”. Using the “update_value” hook provided by Advanced Custom Fields and “wp_update_post” I am able to update the Slug and Post Title using the values given in the respective fields.

<?php
/** Create Title and Slug */
function acf_title( $value, $post_id, $field ) {
if ( get_post_type( $post_id ) === 'staff' ) {
$new_title = get_field( 'first_name', $post_id ) . ' ' . $value;
$new_slug = sanitize_title( $new_title );
wp_update_post(
array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug,
)
);
}
return $value;
}
add_filter( 'acf/update_value/name=last_name', 'acf_title', 10, 3 );

To disable the Title field I remove it from the supports argument for the register_post_type.

'supports' => array( 'editor', 'thumbnail', 'revisions', 'page-attributes' ),

Disabling the title at a custom post type level will, unfortunately, remove the clickable permalink below the title that I often use. To combat that I create a very simple metabox with the permalink. Demonstrated below. (I believe this is no longer the case with Post Types using the Block Editor)

/**
* Adds a meta box
*/
function simple_meta_box() {
add_meta_box( 'prfx_meta', ( 'View Post' ), 'simple_meta_box_callback', 'staff', 'side', 'high' );
}
add_action( 'add_meta_boxes', 'simple_meta_box' );
/**
* Outputs the content of the meta box
*/
function simple_meta_box_callback( $post ) {
echo '<a href="';
the_permalink();
echo '" class="button button-small" target="_blank">View Profile</a>';
}

By:

Published:

Category:


5 responses to “Generate Post Title and Slug from Advanced Custom Fields”

  1. Dan Avatar
    Dan

    please where can i add this code to, using code snippet or directly on the ACF file directory

    1. philhoyt Avatar
      philhoyt

      I would recommend putting this into a custom plugin you maintain (https://www.wpexplorer.com/writing-simple-wordpress-plugin/) or your themes functions.php if its a theme you created.

  2. Rodos Web Design Avatar

    I know its been some years sincei you’ve posted this. But is there a way to add both first and last name to the Update filter? like if any of these 2 change, then do function?

    1. philhoyt Avatar
      philhoyt

      I’ll take a look tonight.

      1. Rodos Web Design Avatar

        Also just one more note, now that i’ve played with your code a bit. When doing the first save, it just puts 1 of the two names in the Title. Second Save adds both. It’s quite wierd and asked this in the ACF forums for a solution.

        Forum thread here: https://support.advancedcustomfields.com/forums/topic/update_post-hook-question/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: