Code Snippet working on one of two sites
-
Hello dear WordPress Community,
i am currently creating a wordpress theme. For that team i have created a custom post type ‘person’. The post type does not support titles, because the title should be dynamically generated out of the acf field for the forname and lastname. My Code is working on one site, the site where i initially tested and developed the theme. After rolling out the theme to another site, i noticed that the title generation with the save_post and the wp_update_post function is not working.Here i my code for the title
function person_acf_custom_title( $post_id ){
if ( ! wp_is_post_revision( $post_id ) and get_post_type() == 'person' and get_the_title() != get_field( 'titel' )." ". get_field( 'vorname' )." ". get_field( 'nachname' ) ){// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'person_acf_custom_title');// update the post, which calls save_post again
// Update post
$my_post = array();
$my_post['ID'] = get_the_ID();
$my_post['post_title'] = get_field( 'titel' )." ". get_field( 'vorname' )." ". get_field( 'nachname' );
$my_post['post_name'] = sanitize_title( $my_post['post_title'] );
// Update the post into the database
wp_update_post( $my_post );remove_action('save_post', 'person_acf_custom_title');
// unhook this function so it doesn't loop infinitely
}
}
add_action('save_post', 'person_acf_custom_title');This is the complete post type registration document:
<?php
if ( ! function_exists('person_post_type') ) {// Register Custom Post Type
function person_post_type() {$labels = array(
'name' => _x( 'Personen', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Person', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Personen', 'text_domain' ),
'name_admin_bar' => __( '', 'text_domain' ),
'archives' => __( '', 'text_domain' ),
'attributes' => __( 'Item Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'Alle Personen', 'text_domain' ),
'add_new_item' => __( 'Neue Person hinzufügen', 'text_domain' ),
'add_new' => __( 'Hinzufügen', 'text_domain' ),
'new_item' => __( 'Neue Person', 'text_domain' ),
'edit_item' => __( 'Person bearbeiten', 'text_domain' ),
'update_item' => __( 'Person aktualisieren', 'text_domain' ),
'view_item' => __( 'Person anschauen', 'text_domain' ),
'view_items' => __( 'Personen anschauen', 'text_domain' ),
'search_items' => __( 'Personen suchen', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Profilbild', 'text_domain' ),
'set_featured_image' => __( 'Profilbild festlegen', 'text_domain' ),
'remove_featured_image' => __( 'Profilbild entfernen', 'text_domain' ),
'use_featured_image' => __( 'Als Profilbild nutzen', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label' => __( 'Person', 'text_domain' ),
'description' => __( 'Personen Register', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'thumbnail' , 'editor' ),
'taxonomies' => array( 'mandate_categories'),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 21,
'menu_icon' => 'dashicons-id-alt',
'show_in_admin_bar' => false,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'personen' ),
);
register_post_type( 'person', $args );}
add_action( 'init', 'person_post_type', 0 );
}function person_taxonomy() {
register_taxonomy(
'mandate_categories', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'person', //post type name
array(
'hierarchical' => true,
'label' => 'Mandate', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'mandate', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'person_taxonomy');function person_acf_custom_title( $post_id ){
if ( ! wp_is_post_revision( $post_id ) and get_post_type() == 'person' and get_the_title() != get_field( 'titel' )." ". get_field( 'vorname' )." ". get_field( 'nachname' ) ){// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'person_acf_custom_title');// update the post, which calls save_post again
// Update post
$my_post = array();
$my_post['ID'] = get_the_ID();
$my_post['post_title'] = get_field( 'titel' )." ". get_field( 'vorname' )." ". get_field( 'nachname' );
$my_post['post_name'] = sanitize_title( $my_post['post_title'] );
// Update the post into the database
wp_update_post( $my_post );remove_action('save_post', 'person_acf_custom_title');
// unhook this function so it doesn't loop infinitely
}
}
add_action('save_post', 'person_acf_custom_title');
?>
- The topic ‘Code Snippet working on one of two sites’ is closed to new replies.