3

In a custom post type I have post titles disabled, however in the list of posts and in the slug it just shows the title as "auto-draft". I want to automatically take two pieces of post-meta and make then the title and the slug.

I thought this would work, but I can't make it happen:

function set_event_title( $data , $postarr ) {
  if($data['post_type'] == 'events') {
    $event_date = get_post_meta($post_id,'event_datetime',true);
    $event_venue = get_post_meta($post_id, 'venue_name' , true);
    $event_title = $event_venue . ' - ' . $event_date;
    $post_slug = sanitize_title_with_dashes ($event_title,'','save');
    $post_slugsan = sanitize_title($post_slug);

    $data['post_title'] = $post_title;
    $data['post_name'] = $post_slugsan;
  }
  return $data;
}

add_filter( 'wp_insert_post_data' , 'set_event_title' , '99', 2 );

Anyone know how I might get it to do its thing correctly?

  • To clarify a little: I want to string two pieces of meta together to make the title and then sanitize that title (twice, actually) to create the slug. – Eckstein Apr 3 '13 at 4:33
3

You are using wrong variable on the following line:

$data['post_title'] = $post_title;

you should use $event_title in $post_title as following:

$data['post_title'] = $event_title;

Also Get Post ID from $postarr parameter.

Updated Code :

function set_event_title( $data , $postarr ) {
  if($data['post_type'] == 'events') {
    $event_date = get_post_meta($postarr['ID'],'event_datetime',true);
    $event_venue = get_post_meta($postarr['ID'], 'venue_name' , true);
    $event_title = $event_venue . ' - ' . $event_date;
    $post_slug = sanitize_title_with_dashes ($event_title,'','save');
    $post_slugsan = sanitize_title($post_slug);

    $data['post_title'] = $event_title;
    $data['post_name'] = $post_slugsan;
  }
  return $data;
}
add_filter( 'wp_insert_post_data' , 'set_event_title' , '10', 2 );

For more information on this filter visit this page.

  • Thanks, that makes sense that I needed to call the right variable containing the post object. This still isn't working for me though... – Eckstein Apr 3 '13 at 4:54
  • @Eckstein Try changing the filter priority. I have changed it in my code. – Vinod Dalvi Apr 3 '13 at 5:01
  • Hmm, still not working. Do I need to use something else besides wp_insert_post_data maybe? – Eckstein Apr 3 '13 at 5:04
  • The code that i have entered as answer is working for me.. – Vinod Dalvi Apr 3 '13 at 5:43
  • ARGR! It was the stupid variable. Thanks for catching it, I hate dumb mistakes. – Eckstein Apr 3 '13 at 6:40
1

I found that the previous answer, as stated in the comments, only works when saving an existing post and not new posts. The below will work for new posts and existing posts.

// func that is going to set our title of our customer magically
function w2w_customers_set_title( $data , $postarr ) {

    // We only care if it's our customer
    if( $data[ 'post_type' ] === 'w2w-customers' ) {

        // get the customer name from _POST or from post_meta
        $customer_name = ( ! empty( $_POST[ 'customer_name' ] ) ) ? $_POST[ 'customer_name' ] : get_post_meta( $postarr[ 'ID' ], 'customer_name', true );

        // if the name is not empty, we want to set the title
        if( $customer_name !== '' ) {

            // sanitize name for title
            $data[ 'post_title' ] = $customer_name;
            // sanitize the name for the slug
            $data[ 'post_name' ]  = sanitize_title( sanitize_title_with_dashes( $customer_name, '', 'save' ) );
        }
    }
    return $data;
}
add_filter( 'wp_insert_post_data' , 'w2w_customers_set_title' , '99', 2 );

It first checks for the title in the $_POST variable, which is where it would come from most of the time. If it is not there, it will pull it from the post_meta, which will handle things like Quick Edits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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