I am trying to:
I read several posts about this, https://toolset.com/forums/topic/trying-to-create-post-title-from-2-custom-fields/ and https://toolset.com/forums/topic/post-link-from-custom-fields/ but was unable to succesfully create a post title for my CPT of Owners by using the custom fields Owner-First-Name and Owner-Last-Name
Here's the code snippet I used:
add_filter( 'wp_insert_post_data' , 'set_owners_title' , '10', 2 );
function set_owners_title( $data , $postarr ) {
if ( 'owners' != $data['post_type'] )
return $data;
$post_title = $data['post_title'];
if ( ! $post_title ) {
$owners_first = trim( $_POST['wpcf']['owner-first-name']);
$owners_last = trim( $_POST['wpcf']['owner-last-name'] );
$owners_title = $owners_first . ' ' . $owners_last;
$post_slug = sanitize_title_with_dashes ($owners_title,'','save');
$post_slugsan = sanitize_title($post_slug);
$data['post_title'] = $owners_title;
$data['post_name'] = $post_slugsan;
}
return $data;
}
add_filter( 'wp_insert_post_data' , 'set_owners_title' , '99', 2 );
apply_filters( 'set_owners_title', $data, $postarr );
Nigel
Supporter
Languages:
English (English )
Spanish (Español )
Timezone:
Europe/London (GMT+00:00)
Hi Larry
The code looks a little confused, if I may say so, you are adding the same filter twice and have a spurious apply_filters call which appears to do nothing.
For a better understanding of how WordPress hooks work you might want to check the plugin handbook: https://developer.wordpress.org/plugins/hooks/
Before commenting further, can I check where the posts are being created? In the front-end with a CRED form or the back-end?
Sorry, these posts are being created on the back-end. I've just unchecked Title in the CPT configuration screen. So when I enter one, it saves with no title.
The code I used was from someone else's similiar issue. I guess it has too many values in it. Can you provide me with the code needed to use the custom fields Owner-First-Name and Owner-Last-Name to successfully create a post title for my CPT of Owners.
Nigel
Supporter
Languages:
English (English )
Spanish (Español )
Timezone:
Europe/London (GMT+00:00)
Hi Larry
I wrote some code that you can try below, but please note that providing or debugging custom code falls outside of our support policy, so I'm providing as-is, I haven't tested it, but I think it should do what you need, and I think it is fairly straightforward to follow the logic.
/**
* Auto-generate post title
*/
function tssupp_autogenerate_title( $post_id, $post ){
if ( 'owner' == $post->post_type ) {
$first_name = get_post_meta( $post_id, 'wpcf-first-name', true );
$last_name = get_post_meta( $post_id, 'wpcf-last-name', true );
$new_title = $first_name . " " . $last_name;
$new_title = sanitize_text_field( $new_title );
$new_slug = sanitize_title( $new_title );
$args = (
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug
);
wp_update_post( $args );
}
}
add_action( 'save_post', 'tssupp_autogenerate_title', 10, 2 );
Thanks so much Nigel! I know you can't assist in debugging the code, but maybe someone else will see this and can assist.
I got this error when implementing your code.
The code snippet you are trying to save produced a fatal error on line 16:
syntax error, unexpected '=>' (T_DOUBLE_ARROW)
Nigel
Supporter
Languages:
English (English )
Spanish (Español )
Timezone:
Europe/London (GMT+00:00)
Hi Larry
Yup, I didn't test it 😉
Line 15 needs editing to:
Hopefully that will do it.