Support Forum
Support › MB Frontend Submission › Trying to set Title from field in CPT From the Custom Fields)Resolved
I was looking at this link: and I am able to set the title to the field with this code BUT when I look at the URL for the post, the URL is ending with no-title when I look at the post from the back end.
Also, I want the post to have the title set to the same field when I save from the back end.
My code for the filter looks like this:
function set_member_title( $data ) {
if($data['post_type'] === 'announcement-cpt') {
$newPostTitle = $_POST['announcement_title'];
$data['post_title'] = $newPostTitle;
return $data;
}
}
add_filter( 'wp_insert_post_data' , 'set_member_title' , '99', 2 );
Looks like it's happening from the front end also.
Hi,
Please refer to this topic to update the post title with a field value https://support.metabox.io/topic/adding-metabox-field-data-to-the-post-title/
On the backend you might need to use the Classic Editor to reload the page and see the changes.
That worked for me after some edits.
Thank you for your reply and answer.
The code you gave me was a start but the slug was missing. This solves that:
add_action( 'rwmb_announcement-cf_after_save_post', 'update_post_title' );
function update_post_title( $post_id ) {
// Get the field value
$my_new_post_title = rwmb_meta( 'announcement_title', '', $post_id );
// generate new post post_slug
$delimiter = "-";
$unwanted_array = ['ś'=>'s', 'ą' => 'a', 'ć' => 'c', 'ç' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ź' => 'z', 'ż' => 'z',
'Ś'=>'s', 'Ą' => 'a', 'Ć' => 'c', 'Ç' => 'c', 'Ę' => 'e', 'Ł' => 'l', 'Ń' => 'n', 'Ó' => 'o', 'Ź' => 'z', 'Ż' => 'z',]; // Polish letters for example
$str = strtr( $str, $unwanted_array );
$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $str))))), $delimiter));
// Preprare update post
$my_post = array(
'ID' => $post_id,
'post_title' => $my_new_post_title,
'post_name' => $slug,
);
wp_update_post( $my_post );
}
Modify the "Unwanted" as you wish of course!