Support Forum
Hi,
I have a snippet that uses the value of custom fields to rename the post when it it saved (published or modified). It works very nicely with simple text fields, but how to pull the value of a Taxonomy field ? I cannot manage to pull the value from the related CPT when the saving of the post is happening.
I would like the new title to be based this way :
{current post's value for custom taxonomy"genre"} {value of custom field (text) "objet_designation"} - {title of related post for CPT "designer"} - {current post's value for custom taxonomy"date"}
This way the generated title would look like this (for example) : Chair "Model A" - Starck Philippe - 1981
It is working except for the related post's title that does not show up (stays blank) : Chair "Model A" - - 1981
Here is the snippet in it's actual form, generating a blank space where I want the related post's title to be used :
add_action( 'save_post_objet', 'update_objet_post_title_with_designer_fixed', 10, 3 );
function update_objet_post_title_with_designer_fixed( $post_id, $post, $update ) {
// Prevent recursion, handle autosaves, revisions, and check for actual update
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE || wp_is_post_revision( $post_id ) || !$update ) {
return;
}
// Assume 'genre', 'date', and 'related_designer_id' are correct field identifiers
$genre_terms = get_the_terms( $post_id, 'genre' );
$genre = !is_wp_error($genre_terms) && !empty($genre_terms) ? $genre_terms[0]->name : '';
$date_terms = get_the_terms( $post_id, 'date' );
$date = !is_wp_error($date_terms) && !empty($date_terms) ? $date_terms[0]->name : '';
$objet_designation = rwmb_meta( 'objet_designation', '', $post_id );
$designer_post_id = rwmb_meta( 'related_designer_id', '', $post_id );
$designer_title = $designer_post_id ? get_the_title($designer_post_id) : '';
// Construct the new title afresh each time
$new_title = trim("$genre \"$objet_designation\" - $designer_title - $date");
// Direct comparison to prevent unnecessary updates
if (get_the_title($post_id) !== $new_title) {
// Temporarily unhook this function to avoid infinite loop
remove_action('save_post_objet', 'update_objet_post_title_with_designer_fixed', 10);
// Update the post
wp_update_post([
'ID' => $post_id,
'post_title' => $new_title,
// Optionally update the slug as well
'post_name' => sanitize_title($new_title),
]);
// Re-hook this function
add_action('save_post_objet', 'update_objet_post_title_with_designer_fixed', 10, 3);
}
}
The post title is wrong, I do not need help anymore with pulling the taxonomy values (I have managed that in the provided above code).
My only issue is with pulling the current post's related post's title, and inserting it in the newly generated title (before the date).
Hello,
Which is the field type related_designer_id
? Is this the post field? Please share some screenshots of the field settings.
Hi Peter,
The code is generated by ChatGPT, it invented some name for a custom field that does not exist (for the relationship), since the data I want to pull is coming from the Meta Box created Relationship (between two CPTs : "objet" and "designers").
So the question again : how can you pull the data of a relationship while in the admin area and saving a post ?
So basically, where the snippet above has this line :
$designer_post_id = rwmb_meta( 'related_designer_id', '', $post_id );
it should be able to pull the value of the related post's title and insert it here.
Hello,
The helper function rwmb_meta() helps you to get the value of the field ID, it's not the relationship. If you want to get the relationship, please follow the documentation
https://docs.metabox.io/extensions/mb-relationships/#getting-connected-items
Note: supporting a customization code is beyond our scope of support. If you cannot complete the task, please contact us here https://metabox.io/contact/. We offer a customization service with an extra fee.
IO figured it out on my own (thanks ChatGPT) and ended up with this function that works perfectly, some might find it useful :
<?php
function update_objet_post_title_with_designer_and_latest_values($post_id, $post, $update) {
if ($post->post_type !== 'objet' || wp_is_post_revision($post_id) || !$update) {
return;
}
$genre_terms = get_the_terms($post_id, 'genre');
$genre = !is_wp_error($genre_terms) && !empty($genre_terms) ? $genre_terms[0]->name : '';
$date_terms = get_the_terms($post_id, 'date');
$date = !is_wp_error($date_terms) && !empty($date_terms) ? $date_terms[0]->name : '';
$objet_designation = get_post_meta($post_id, 'objet_designation', true);
$designer_title = '';
$connected_designers = MB_Relationships_API::get_connected([
'id' => 'objet-designer',
'from' => $post_id,
]);
foreach ($connected_designers as $designer) {
// Assuming the designer's name is stored in "last, first" format.
$name_parts = explode(', ', $designer->post_title);
if (count($name_parts) == 2) {
$designer_title = trim($name_parts[1]) . ' ' . trim($name_parts[0]);
} else {
$designer_title = $designer->post_title; // Use original if not in expected format.
}
// Sanitize for safety while preserving accents and capital letters.
$designer_title = wp_kses_post($designer_title);
break;
}
$designer_part = $designer_title ? " - $designer_title" : '';
$new_title = trim("$genre \"$objet_designation\"$designer_part - $date");
$new_title = rtrim($new_title, " -");
if (get_the_title($post_id) !== $new_title) {
remove_action('wp_after_insert_post', 'update_objet_post_title_with_designer_and_latest_values', 10);
// Sanitize slug for URL, ensuring accents are handled appropriately.
$sanitized_slug = sanitize_title($new_title);
wp_update_post([
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $sanitized_slug,
]);
add_action('wp_after_insert_post', 'update_objet_post_title_with_designer_and_latest_values', 10, 3);
}
}
add_action('wp_after_insert_post', 'update_objet_post_title_with_designer_and_latest_values', 10, 3);
I think it would be so great if MetaBox added a feature that allows to select custom fields to create the Post title. I will copy this message to the FB group, that is where I have seen my feature request taken in account by Anh Tran (I believe he likes good ideas :-).