Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logichub/e6347a12a69b156aada1a2147106ed96 to your computer and use it in GitHub Desktop.
Save logichub/e6347a12a69b156aada1a2147106ed96 to your computer and use it in GitHub Desktop.
Using Advanced Custom Fields to create Post Title & Slug for different CPTs
<?php
// Auto fill Title and Slug for 'companies' CPT
function acf_title_companies( $value, $post_id, $field ) {
if ( get_post_type( $post_id ) == 'companies' ) {
$new_title = get_field( 'company_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=company_name', 'acf_title_companies', 10, 3 );
// Auto fill Title and Slug for 'contacts' CPT
function acf_title_contacts( $value, $post_id, $field ) {
if ( get_post_type( $post_id ) == 'contacts') {
$new_title = get_field( 'name_first', $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=name_first', 'acf_title_contacts', 10, 3 );
// Auto fill Title and Slug for 'properties' CPT
function acf_title_properties( $value, $post_id, $field ) {
if ( get_post_type( $post_id ) == 'properties') {
$new_title = get_field( 'building_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=building_name', 'acf_title_properties', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment