Home › Forums › General Issues › Create/update post title from ACF fields › Reply To: Create/update post title from ACF fields
Elliot! you’re a hero!
Stupid of me not to think about debugging first! You were right on point about that $target_product
was not a single, but an array of post objects!
so using this updated variable $review_target_product = $target_product['0']->ID;
made my code work!
For anyone interested in the future, this is all the code I used to construct my post titles in my functions.php
//Auto add and update Title field:
function my_post_title_updater( $post_id ) {
$my_post = array();
$my_post['ID'] = $post_id;
$manufacturer = get_field('manufacturer');
$target_product = get_field('target_product');
$review_target_product = $target_product['0']->ID;
$manufacturer_target = get_field('manufacturer', $review_target_product);
if ( get_post_type() == 'manufacturer' ) {
$my_post['post_title'] = get_field('manufacturer_name');
} elseif ( get_post_type() == 'products' ) {
$my_post['post_title'] = get_field('kitName') . ' (' . get_field('manufacturer_name', $manufacturer->ID) . ' ' . get_field('kitNumber') . ')';
} elseif ( get_post_type() == 'reviews' ) {
$my_post['post_title'] = get_field('kitName', $review_target_product) . ' (' . get_field('manufacturer_name', $manufacturer_target) . ' ' . get_field('kitNumber', $review_target_product) . ')';
}
// Update the post into the database
wp_update_post( $my_post );
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.