Forked from philhoyt/advanced-custom-fields-post-title.php
Last active
September 9, 2022 10:30
-
-
Save logichub/e6347a12a69b156aada1a2147106ed96 to your computer and use it in GitHub Desktop.
Using Advanced Custom Fields to create Post Title & Slug for different CPTs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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