Skip Navigation

[Resolved] Use Custom Fields to set Post Title and Post Slug

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to use custom fields to set post title and post slug from wp-admin section.

Solution:
In this case, user is using number, type and year custom fields to generate the post title and slug. But user has some issue with his code.

You can find the solution with the following reply:
https://toolset.com/forums/topic/use-custom-fields-to-set-post-title-and-post-slug/#post-369037

Relevant Documentation:

This support ticket is created 8 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by arvidB 8 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#368883

Triggered by a previous support thread ( https://toolset.com/forums/topic/set-post-titleslug-based-on-custom-fields/ ) I started looking into this.

I want to use three custom fields in my CPT called issue. the three custom fields are;
- number
- type
- year
These three custom fields together will form the title and the slug.

Now I created the following code in /wp-include/post.php;

function set_issue_title( $data , $postarr ) {
  if($data['post_type'] == 'issue') {
    $issue_number = get_post_meta($postarr['ID'],'wpcf[number]',true);
    $issue_type = get_post_meta($postarr['ID'], 'wpcf[type]' , true);
    $issue_year = get_post_meta($postarr['ID'], 'wpcf[year]' , true);
    $issue_title = $issue_type . ' - ' . $issue_year . ' - ' . $issue_number;
    $post_slug = sanitize_title_with_dashes ($issue_title,'','save');
    $post_slugsan = sanitize_title($post_slug);

    $data['post_title'] = $issue_title;
    $data['post_name'] = $post_slugsan;
  }
  return $data;
}
add_filter( 'wp_insert_post_data' , 'set_issue_title' , '99', 3 );
apply_filters( 'set_issue_title', $data, $postarr );

Unfortunately nothing happens when I publish or save a draft. Any pointers on what might be wrong?

Thanks!

Arvid

#368934

I rewrote some of the code and placed it in the functions.php of my theme. Now the draft is saved with dashes in the title, but not the values from the fields. How can I figure out what the names of the fields are?

Fields I'm using have the following IDs;
wpcf[number]
wpcf[type]
wpcf[year]


add_filter( 'wp_insert_post_data' , 'set_issue_title' , '10', 3 );

function set_issue_title( $data , $postarr ) {
if ( 'issue' != $data['post_type'] )
return $data;

$post_title = $data['post_title'];

if ( ! $post_title ) {

$issue_number = trim( $_POST['wpcf[number]'] );	
$issue_type = trim( $_POST['wpcf[type]'] );
$issue_year = trim( $_POST['wpcf[year]'] );

$issue_title = $issue_type . ' - ' . $issue_year . ' - ' . $issue_number;
$post_slug = sanitize_title_with_dashes ($issue_title,'','save');
$post_slugsan = sanitize_title($post_slug);

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

return $data;
}

#368953

I was able to validate the rest of the code is working. Sample data stores the post with the correct title and slug;

$issue_number = "12";
$issue_type = "DD-weekblad";
$issue_year = "1982";

The issue is how I get the variables from the post data;

$issue_number = trim( $_POST['wpcf[number]'] );	
$issue_type = trim( $_POST['wpcf[type]'] );
$issue_year = trim( $_POST['wpcf[year]'] );

I tried the following

$issue_number = trim( $_POST['wpcf[number]'] );
$issue_number = trim( $_POST['wpcf-number'] );
$issue_number = trim( $_POST['number'] );

And none is working or giving the expected result.

I was able to determine that the data is available in the post data;
[wpcf] => Array ( [year] => 2015 [number] => 12 [type] => dd-weekblad [barcode] => 12 )

Just for some strange reason, I'm unable to take the array and trim it into the variables $issue_number, $issue_type and $issue_year.

#369037

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Could you please try following code:

$issue_number = trim( $_POST['wpcf']['number']); 
$issue_type = trim( $_POST['wpcf']['type'] );
$issue_year = trim( $_POST['wpcf']['year'] );

I hope this will fix your issue and you'll be able to grab the post fields value.

#369145

Thanks Minesh, this indeed resolved my issue. Thank you so much.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.