Support Forum
Support › MB Settings Page › Update post title from settings page custom field.
Hi Metabox,
Not sure what this is the best way to achieve what I am doing but, I want to create multiple posts under a CPT called 'Services' (slug = services). The post title will combine two separate fields {service_name_1} and {location_name_1} that are populated in a settings. There is also service_name_2, service_name_3 etc. and the same for locations. So ideally it will create:
service_name_1 + location_name_1
service_name_2 + location_name_1
service_name_3 + location_name_1
service_name_1 + location_name_2
service_name_2 + location_name_2
etc.
The only way I can think to do it, is create 6 draft posts titles and add a string in the post title of {service_name_1} In {location_name_1}
And when the custom field is saved it will loop through all the posts inside the service CPT and change the string to the value of what was saved inside the settings page custom field.
I have tried two variations, but for some reason it does not update the post title. Here are the two variations:
Variation 1:
// Add the action hook to update the target field after custom field save
add_action('rwmb_after_save_field', 'update_titles_and_slugs_in_services_cpt', 10, 5);
// Define the update_titles_and_slugs_in_services_cpt function
function update_titles_and_slugs_in_services_cpt($null, $field, $new, $old, $object_id) {
// Check if the custom field being saved is the one you want
if ($field['id'] === 'service_details_service_1') {
// Get the custom field value
$custom_field_value = $new;
// Check if the custom field value is not empty
if (!empty($custom_field_value)) {
// Get all posts in the 'services' custom post type
$services_posts = get_posts(array(
'post_type' => 'services',
'posts_per_page' => -1,
));
// Loop through each services post and update its title and slug
foreach ($services_posts as $services_post) {
// Update the post title with the new custom field value
$new_title = str_replace('xyxy', $custom_field_value, $services_post->post_title);
// Update the post title and slug only if they have changed
if ($new_title !== $services_post->post_title) {
$post_data = array(
'ID' => $services_post->ID,
'post_title' => $new_title,
'post_name' => sanitize_title($new_title), // Update the slug
);
// Update the post
wp_update_post($post_data);
}
}
}
}
}
Variation 2
// Hook into rwmb_after_save_field action
add_action( 'rwmb_after_save_field', 'update_post_titles_with_custom_field', 10, 5 );
function update_post_titles_with_custom_field( $null, $field, $new, $old, $object_id ) {
// Check if the saved field is 'service_details_service_1' and is Admin
if ( $field['id'] === 'service_details_service_1' && is_admin() ) {
// Get all 'services' custom posts
$service_args = array(
'post_type' => 'services',
'posts_per_page' => -1,
);
$service_posts = get_posts( $service_args );
foreach ( $service_posts as $service_post ) {
// Get the current post title
$post_title = get_the_title( $service_post->ID );
// Check if the placeholder is present in the post title
if ( strpos( $post_title, '{service_details_service_1}' ) !== false ) {
// Replace the placeholder with the new field value
$updated_title = str_replace( '{service_details_service_1}', $new, $post_title );
// Only update if the title has changed
if ( $updated_title !== $post_title ) {
$post_data = array(
'ID' => $service_post->ID,
'post_title' => $updated_title,
);
// Update the post title and slug
wp_update_post( $post_data );
}
}
}
}
}
I have checked the CPT slug is correct & also the custom field ID is correct. But still not working.
Any help would be great.
Thanks
Hello,
It looks so complicated. I think you can try to update the post title without the condition
if ($new_title !== $services_post->post_title)
in variation 1 and see how it works. If it still does not work, please contact us here https://metabox.io/contact/
We offer customization services and our development team can help you with an extra fee.