admin管理员组文章数量:1124076
I have tried a few versions of the code to get this working and seem to come up with a couple of errors either way.
I am using ACF for the fields and adding the below code to the themes functions.php file, the code was taken from the ACF forum and changed to suit my field names.
Can anyone spot what may be causing the error?
function ccc_acf_update_fixture_post_title( $post_id, $post) {
if ( get_post_type( $post_id ) == 'fixture' ) {
$team = get_field( 'team', $post_id );
$venue = get_field( 'venue', $post_id );
$opponent = get_field( 'opponent', $post_id );
$date = new DateTime(get_field( 'date', $post_id ));
$new_title = $team . ' - ' . $venue . ' Vs ' . $opponent . ' on ' . $date->format('d/m/Y');
$new_slug = sanitize_title( $new_title );
$args = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug,
);
}
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'ccc_acf_update_fixture_post_title', 30, 2 );
// update the post, which calls save_post again
wp_update_post( $args );
// re-hook this function
add_action('save_post', 'ccc_acf_update_fixture_post_title', 30, 2 );
}
add_action( 'save_post', 'ccc_acf_update_fixture_post_title', 30 );
The image or error is below...
Any help on this will be appreciated.
Craig
I have tried a few versions of the code to get this working and seem to come up with a couple of errors either way.
I am using ACF for the fields and adding the below code to the themes functions.php file, the code was taken from the ACF forum and changed to suit my field names.
Can anyone spot what may be causing the error?
function ccc_acf_update_fixture_post_title( $post_id, $post) {
if ( get_post_type( $post_id ) == 'fixture' ) {
$team = get_field( 'team', $post_id );
$venue = get_field( 'venue', $post_id );
$opponent = get_field( 'opponent', $post_id );
$date = new DateTime(get_field( 'date', $post_id ));
$new_title = $team . ' - ' . $venue . ' Vs ' . $opponent . ' on ' . $date->format('d/m/Y');
$new_slug = sanitize_title( $new_title );
$args = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug,
);
}
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'ccc_acf_update_fixture_post_title', 30, 2 );
// update the post, which calls save_post again
wp_update_post( $args );
// re-hook this function
add_action('save_post', 'ccc_acf_update_fixture_post_title', 30, 2 );
}
add_action( 'save_post', 'ccc_acf_update_fixture_post_title', 30 );
The image or error is below...
Any help on this will be appreciated.
Craig
Share Improve this question asked Mar 10, 2024 at 21:36 Craig WatsonCraig Watson 113 bronze badges 3 |2 Answers
Reset to default 0I just wanted to post the code that got this working without issues. I had an extra argument in the function that was never being used ( $post
).
function ccc_acf_update_fixture_post_title( $post_id ) {
if ( get_post_type( $post_id ) == 'fixture' ) {
$team = get_field( 'team', $post_id );
$venue = get_field( 'venue', $post_id );
$opponent = get_field( 'opponent', $post_id );
$date = new DateTime(get_field( 'date', $post_id ));
$new_title = $team . ' - ' . $venue . ' Vs ' . $opponent . ' on ' . $date->format('d/m/Y');
$new_slug = sanitize_title( $new_title );
$args = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug,
);
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'ccc_acf_update_fixture_post_title' );
// update the post, which calls save_post again
wp_update_post( $args );
// re-hook this function
add_action('save_post', 'ccc_acf_update_fixture_post_title');
}
}
add_action( 'save_post', 'ccc_acf_update_fixture_post_title' );
I'm still happy to take feedback if this function could be improved.
If you using acf so then please try to changed
Changed the action hook to acf/save_post to ensure ACF fields are saved before updating the post title.
Replace this line code
add_action( 'save_post', 'ccc_acf_update_fixture_post_title' );
to
add_action( 'acf/save_post', 'ccc_acf_update_fixture_post_title', 20 );
本文标签: Generate Post Title From ACF Fields on Custom Post Type
版权声明:本文标题:Generate Post Title From ACF Fields on Custom Post Type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736616961a1945492.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$post
argument from the function and this got things working as expected. Thank you! – Craig Watson Commented Mar 13, 2024 at 9:09$post
and$post_id
in your function, but if you did you'd have to just change theget_field()
toget_field( 'team', $post->ID )
. But I think the way you have it is better because you don't need all the post data, just the IDs. Would love to see the work you're doing - I've been working on a football league management system for adult/sunday leagues since 2021. Launched it in 2022 and just keep adding features. Completely custom. bramptonsoccer.com The whole league management aspect is pretty much automated. – Tony Djukic Commented Mar 13, 2024 at 19:32