CPT is designed in a way to force the use of title in your case you need the client name and the title so you would end up entering the data twice which is a bit of a double effort to be frank.
I have added a small plugin that will allow you to enter the client name and have it do the title automatically.
/**
* Plugin Name: CC Clients Customizations
* Description: Customizes the CC Clients post type, setting post titles automatically from the 'nombre_cliente' custom field.
* Version: 1.0
* Author: Tarek Tarabichi
*/
// Function to automatically update post title based on the 'nombre_cliente' custom field.
function custom_field_as_post_title_for_clients( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( 'cc_clientes' != get_post_type( $post_id ) ) return;
$nombre_cliente = get_post_meta( $post_id, 'nombre_cliente', true );
if ($nombre_cliente && get_the_title($post_id) !== $nombre_cliente) {
remove_action('save_post', 'custom_field_as_post_title_for_clients');
wp_update_post( array(
'ID' => $post_id,
'post_title' => $nombre_cliente,
) );
add_action('save_post', 'custom_field_as_post_title_for_clients');
}
}
add_action( 'save_post', 'custom_field_as_post_title_for_clients' );
// Function to automatically update Invoice post title based on the 'cliente_factura'+numero_factura+date custom field.
function update_cc_invoice_title_on_save( $post_id ) {
if ( 'cc_invoices' != get_post_type( $post_id ) ) return;
// Avoid recursion
remove_action('save_post', 'update_cc_invoice_title_on_save');
// Get metadata values
$cliente_factura_id = get_post_meta( $post_id, 'cliente_factura', true );
$numero_factura = get_post_meta( $post_id, 'numero_factura', true );
// Fetch the client's name using the ID
$cliente_factura_name = get_the_title( $cliente_factura_id ); // Assuming it's a post reference
// Fetch other necessary values similarly
// For example, if numero_factura needs conversion from ID to value, perform it here
$publish_date = get_the_date( 'Y-m-d', $post_id ); // Format as needed
$new_title = $cliente_factura_name . ' - ' . $numero_factura . ' - ' . $publish_date;
// Update the post
wp_update_post( array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => sanitize_title($new_title) // Update slug
));
// Re-hook this function
add_action('save_post', 'update_cc_invoice_title_on_save');
}
add_action( 'save_post', 'update_cc_invoice_title_on_save' );
// Hook into the save action.
add_action( 'save_post', 'update_cc_invoice_title_on_save' );
CPT is designed in a way to force the use of title in your case you need the client name and the title so you would end up entering the data twice which is a bit of a double effort to be frank. I have added a small plugin that will allow you to enter the client name and have it do the title automatically.
// Function to automatically update post title based on the 'nombre_cliente' custom field. function custom_field_as_post_title_for_clients( $post_id ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( 'cc_clientes' != get_post_type( $post_id ) ) return; $nombre_cliente = get_post_meta( $post_id, 'nombre_cliente', true ); if ($nombre_cliente && get_the_title($post_id) !== $nombre_cliente) { remove_action('save_post', 'custom_field_as_post_title_for_clients'); wp_update_post( array( 'ID' => $post_id, 'post_title' => $nombre_cliente, ) ); add_action('save_post', 'custom_field_as_post_title_for_clients'); } } add_action( 'save_post', 'custom_field_as_post_title_for_clients' );
// Function to automatically update Invoice post title based on the 'cliente_factura'+numero_factura+date custom field. function update_cc_invoice_title_on_save( $post_id ) { if ( 'cc_invoices' != get_post_type( $post_id ) ) return;
// Hook into the save action. add_action( 'save_post', 'update_cc_invoice_title_on_save' );