How to set post title on save/publish from meta values?

Support General How to set post title on save/publish from meta values?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #5431
    techdudetechdude
    Participant

    Hi! I am realizing a custom season / match schedule for a ice hockey team and results for played games with a own custom post type. So everythings fine so far but now it would be killer to have the post title set to "team1 - team2, date" on save/publish. That would save a ton of time while adding the season shedule and would make real sense. Thanks for any idea!

    #5452
    Anh TranAnh Tran
    Keymaster

    Hi,

    This can be done with filter wp_insert_post_data. I answered a similar question on StackOverflow. You can access to meta fields via $_POST.

    PS: The reason you should not use save_post or these actions because using them require to call wp_update_post function, which causes an infinite loop of saving data. Using wp_insert_post_data makes sure you update the data before the post is updated and thus, no infinite loop.

    #6271
    jcleavelandjcleaveland
    Participant

    Hi,

    I am using this method to join two meta fields (first and last name) and insert them as the title. I am using a front-end form to make changes. The front-end form works well and all of the data is captured as expected. I am having problems with the function to insert the post title though. I am using this:

    function set_member_meta_title( $data , $postarr ) {
      if($data['post_type'] == 'obh_member_meta') {
        $first_name = get_post_meta($postarr['ID'],'obh_member_first_name',true);
        $last_name = get_post_meta($postarr['ID'], 'obh_member_last_name' , true);
        $title = $first_name . ' ' . $last_name;
        $post_slug = sanitize_title_with_dashes ($title,'','save');
        $post_slugsan = sanitize_title($post_slug);
    
        $data['post_title'] = $title;
        $data['post_name'] = $post_slugsan;
      }
      return $data;
    }
    add_filter( 'wp_insert_post_data' , 'set_member_meta_title' , '500', 2 );

    The funny thing is that it inserts the last value for the fields. For instance if I save Joe as the first name, it does nothing, but when I go back and change Joe to Frank, it writes Joe as the title while changing the actual first name in the DB to Frank.

    I thought it was a priority issue so I have played with them from 1-999 with little to no effect.

    I'm at a loss. Any thoughts?

    Thanks!

    #6273
    Anh TranAnh Tran
    Keymaster

    Hi,

    I think the problem is the order of execution.

    The wp_insert_post_data runs before the post meta is saved. So, first time you submit, post meta is blank (it hasn't been save), and the post title is blank. After submission, Joe is saved. And when you submit the 2nd time, it gets from post meta (now it is the previous value "Joe") and you get it as the post title.

    The sequence looks like this:

    1. Filter post data with wp_insert_post_data (where you change the post title)
    2. Save post data
    3. Save post meta (where your meta are saved)

    So, in this case, you should not get post meta via get_post_meta to apply to post title. Instead, you can get the new submitted post meta via $_POST['obh_member_first_name'].

    Hope that makes sense.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘How to set post title on save/publish from meta values?’ is closed to new replies.