1

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... Image of WordPress error message

Any help on this will be appreciated.

Craig

3
  • 1
    Is his football related by any chance? Can you also isolate what's exactly is on line 34 of your file. I'm sure the line count in your snippet isn't the same as the file itself, so basically on line 34, whatever you have there, it's expecting multiple arguments but is only receiving one. If we can isolators what exactly that is this should be solvable. (With stuff like ACF that is constantly under development and changing, you can often find snippets for older versions that no longer work.) Commented Mar 11 at 18:30
  • @TonyDjukic This one is Cricket-related, but the post type would work for Football in principle. I did some further research into 'save_post' and from your mention of the issues being thrown on line 34. I removed the $post argument from the function and this got things working as expected. Thank you! Commented Mar 13 at 9:09
  • I suspect you could swap $post and $post_id in your function, but if you did you'd have to just change the get_field() to get_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. Commented Mar 13 at 19:32

2 Answers 2

0

I 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.

0

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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.