Amazing work, thanks. Works a charm.
As per ”, I should have done the following code works…
<?php
// Define custom query parameters
$custom_query_args = array (
'post_type' => array( 'post' ),
'category_name' => 'thebigshow',
'pagination' => false,
'posts_per_page' => '-1',
'order' => 'DESC',
);
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Output custom query loop
if ( $custom_query->have_posts() ) :
$track_number = 0;
echo '<table class="sortable">';
echo '<tr>';
echo '<th>ID</th>';
echo '<th>Artist</th>';
echo '<th>Track</th>';
echo '<th>Date</th>';
echo '<tr>';
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
if( have_rows('track_listing') ):
$tracks_forward = get_field('track_listing');
$tracks_reversed = array_reverse($tracks_forward);
// loop through the rows of data
foreach ($tracks_reversed as $track):
$track_number = $track_number+1;
echo '<tr>';
// display a sub field value
echo '<td class="track_number">';
echo $track_number;
echo '</td>';
echo '<td>';
echo $track['track_artist'];
echo '</td>';
echo '<td>';
echo $track['track_title'];
echo '</td>';
echo '<td>';
echo get_post_time('jS M Y');
echo '</td>';
echo '</tr>';
endforeach;
else :
// no rows found
endif;
endwhile;
echo '</table>';
endif;
// Reset postdata
wp_reset_postdata();
?>
Within the foreach, how do I access the_sub_field(‘foo’)?
Excellent. I’ll give it a test!
OK. I followed the instructions in the post linked above and it worked fine. I had to re-run the query for the likes of…
So if anyone can note the SQL code to include such additions, for future reference, it would be much appreciated.
Thanks,
Andy.
Found a relevant link. Is the process outlined by @elliot still best practice in ACF Pro? http://support.advancedcustomfields.com/forums/topic/best-practice-for-changing-custom-fields/
To paraphrase @elliot, [write SQL in phpMyAdmin to the tune of…]
UPDATE wp_postmeta
SET meta_key = 'new_field_name'
WHERE meta_key = 'old_field_name';
and
UPDATE wp_postmeta
SET meta_key = '_new_field_name'
WHERE meta_key = '_old_field_name';
This is what it looks like in the wp_posts table for a single post being published.

Also, so far as the post_id jumping by 3 is concerned; I think each revision creates a new post_id. And whilst I’m only clicking ‘Publish’ once, three different ‘revisions’ are being made.
It’s all backend and it’s only the ACF data being changed that makes it go up by 3.
That said, with the exception of the post number jumping by 3, moving the wp_update_post( $my_post ); within each if/else statement; seems to have fixed the initial duplicates issue.
OK… I’ve commented out the two ‘elseif‘ elements (as they are not currently setup for dealing with variables pre $_POST[‘acf’]). But what I’ve done is move the wp_update_post( $my_post ) to within the if statement. Can anyone confirm this is expected behaviour?
Also, I’ve noted that the post_id’s when I’m adding new ‘testimonials’ are going up in 3’s. I.e. 84, 87, 90. Is this also expected behaviour, or indicative of an issue?
// ACF Auto Titles
function my_post_title_updater( $post_id ) {
if ( get_post_type( $post_id ) == 'testimonial' ) {
$name_field = $_POST['acf'][field_556ea5ac8c15b]; // Name
$testimonial_field = $_POST['acf'][field_556ea5c68c15c]; // Testimonial
$my_post = array(
'ID' => $post_id,
'post_title' => $name_field . ' - \'' . $testimonial_field . '\'',
'post_name' => $post_id
);
wp_update_post( $my_post );
/* } elseif ( get_post_type( $post_id ) == 'client' ) {
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = get_field( 'name', $post_id );
$my_post['post_name'] = $my_post['post_title'];
wp_update_post( $my_post );
} elseif ( get_post_type( $post_id ) == 'project' ) {
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = get_field( 'name', $post_id );
$my_post['post_name'] = $my_post['post_title'];
wp_update_post( $my_post );
*/ }
}
add_action('acf/save_post', 'my_post_title_updater', 1);
I commented out the action and it no longer repeats (when editing a ‘testimonial’ post type), so it must be something in the function.
I have started changing the function to run ahead of $_POST[‘acf’], but the issue remains. Please see my updated code below…
// ACF Auto Titles
function my_post_title_updater( $post_id ) {
if ( get_post_type( $post_id ) == 'testimonial' ) {
$name_field = $_POST['acf'][field_556ea5ac8c15b]; // Name
$testimonial_field = $_POST['acf'][field_556ea5c68c15c]; // Testimonial
$my_post = array(
'ID' => $post_id,
'post_title' => $name_field . ' - \'' . $testimonial_field . '\'',
'post_name' => $post_id
);
} elseif ( get_post_type( $post_id ) == 'client' ) {
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = get_field( 'name', $post_id );
$my_post['post_name'] = $my_post['post_title'];
} elseif ( get_post_type( $post_id ) == 'project' ) {
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = get_field( 'name', $post_id );
$my_post['post_name'] = $my_post['post_title'];
}
wp_update_post( $my_post );
}
add_action('acf/save_post', 'my_post_title_updater', 1);
Any luck Calmah?
+1
I started a thread that fleshed out my thoughts on this.
http://support.advancedcustomfields.com/forums/topic/reusing-flexible-content-fields/
OK, I found out what I was doing wrong. I was using the_field, when I should have been using ‘get_field’. Thanks.
I wasn’t setting the ID into the my_post array.
I’ve added that in and it works now for both new and updated posts. Weird how it updated for existing items but now new items.
if ( get_post_type( $post_id ) == 'equipment' ) {
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = get_field( 'name', $post_id );
wp_update_post( $my_post );
}
Hi Elliot,
I’ve updated accordingly, but the problem remains. I’ve made a video of how it behaves and copied the code I’m using below. It all seems pretty kosher. Could it be something to do with the post_id not being ready for new posts?
http://www.youtube.com/watch?v=tzkWP_SBRjU
function my_post_title_updater( $post_id ) {
if ( get_post_type( $post_id ) == 'testimonial' ) {
$my_post = array();
$my_post['post_title'] = get_field( 'name', $post_id ) . ' - "' . substr(get_field( 'testimonial', $post_id ), 0, 30) . '..."';
wp_update_post( $my_post );
}
if ( get_post_type( $post_id ) == 'equipment' ) {
$my_post = array();
$my_post['post_title'] = get_field( 'name', $post_id );
wp_update_post( $my_post );
}
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
Thanks,
Andy.
I was a little too early with the champagne…
My function only appears to update existing posts correctly, not new posts. If I remove the function, create a new post, add the function in again and re-save, it works. Just not ‘off the bat’.
Does anyone know which direction I should be looking in to resolve this?
Thanks,
Andy.
Hi,
This appears to do the job. Expanding on the theme of testimonials, it will update the actual WP post title based on two ACF fields, ‘name’ and ‘testimonial’. It will set the title as follows…
NAME – “First 20 characters…”
function my_post_title_updater( $post_id ) {
if ( get_post_type() == 'testimonial' ) {
$my_post = array();
$my_post['post_title'] = get_field( 'name', $post_id ) . ' - "' . substr(get_field( 'testimonial', $post_id ), 0, 30) . '..."';
// 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);
You could sharpen up the shortening of the testimonial, but everything I need to solve the challenge I had, seems to be achievable with this code.
For prosperity, I’m using the ‘Custom Post Type UI‘ plugin to register the custom post types.
Thanks for everyones help and input.
@Ionut Staicu – Does the code you provided just go in the functions.php?
Hi,
@iamntz – How would you implement that?
For prosperity – A similar themed thread http://support.advancedcustomfields.com/forums/topic/replacing-custom-post-type-post-title-with-an-acf/
Thanks,
Andy.
Hi,
True, but I can think of a few situations where it may be beneficial to have some control of the title from within ACF.
Control – To be able to append / prepend details to the title. Or to validate data (such as when you only want a drop down date entered – separate to the core date).
Styling – To be able to control the look of title in the backend with similar ease as ACF. (Such as adding a ‘title’ to the title field.)
Position – From the end users POV, it may make more sense to have the title field positioned between other fields.
After sleeping on it, it may be useful to have the title generated from a combination of fields. So a ‘testimonial’ custom field group may save the title from a combination of fields. From a name field, followed by a separator, then the first 5 words from a testimonial field. (Less for use in the front end, more for ease of management in the backend).
What do you think?
Fantastic plugin, beautiful site. Keep up the great work!
Thanks,
Andy.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.