Skip Navigation

[Resolved] Use custom field value in custom type post title.

This support ticket is created 4 years, 8 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 14 replies, has 2 voices.

Last updated by Himanshu Agarwal 4 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#1558179

Hello,

I have created a custom post type with name Courses. Also i have created a custom fields in it.

I dont want to use WordPress default title field and visual editor for this custom post type only.

I have remove visual editor from custom post edit section but not able to remove or hide default title fields.

Now following things i want to do:-

1. Remove or hide default title field for only custom post type.
2. Use a custom field value as title. (Automatically put a custom field value in title, whenever we see all post section it show that custom field value as title but do not show when we edit or create a new post in that custom post type.).
3. In short, when we save a post , custom field value goes into title field no matter field is filled or not.

Please help me to achieve this. Thank You

#1559403

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

First of all, I would like to know that you want to remove the default post title field when you add/edit the post from backend admin - is that correct? If yes, if you want to disable the default post title field then you should just edit your post type from Toolset => Post Types => Edit your post type => deselect the checkbox for title field from the section "Sections to display when editing ".

OR

If you do not want to disable the title field but wanted to assign the custom field value to it, you can use the default save_post action. Please check the following related doc with an example:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/#an-example

#1565267

Hello, I searched a little bit more and tried this code.

function set_event_title( $data , $postarr ) {
  if($data['post_type'] == 'course-apply') {
    $starts = get_post_meta($postarr['ID'],'wpcf-starting-date',true);
    $starts= date('M j', $starts);
    $ends = get_post_meta($postarr['ID'], 'wpcf-ending-date' , true);
    $ends= date('M j, Y', $ends);
    $waht_for = get_post_meta($postarr['ID'], 'wpcf-for' , true);
    $event_title = $starts.' - ' . $ends.' ('.$waht_for.')';
    $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 );

But this code giving me error.

When i save or publish first time then its giving me error that
date() expects parameter 2 to be integer, string given
But when i refresh error page its working fine and putting custom-field value in title field.

Do you have any clue that why its giving error for first time and working fine on refresh?
Can you please check and let me know if code need some modification?
Please help me to solve this.

#1565269

Minesh
Supporter

Languages: English (English )

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

The hook you are using is a WordPress hook. In addition to that, I see you added single quotes to priority '10', you need to add without single quotes 10 to your hook.

However - I suggest using the following hook as per the Toolset standard.
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/#an-example

Please check the following related tickets that may help you:
=> https://toolset.com/forums/topic/modify-url-of-posts-want-to-use-a-prefix/#post-1238687
=> https://toolset.com/forums/topic/add-auto-generate-post-title-from-fields/#post-1213248

#1565317

What will be we use for for adding title like this?
update_post_meta( $post_id, 'wpcf-overdue', 1 );

#1565321

Minesh
Supporter

Languages: English (English )

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

As I understand, you want to update the post title - if yes, please check the solution given with the following related ticket:
=> https://toolset.com/forums/topic/add-auto-generate-post-title-from-fields/#post-1213248

#1565339

Hii, thank you for quick reply,

I tried with your suggested solution.

function swp_autogenerate_title( $post_id, $post ){
       
  if ( 'course-apply' == $post->post_type ) {
    $starts = get_post_meta($post_id,'wpcf-starting-date',true);
    $starts= date('M j', $starts);
    $ends = get_post_meta($post_id, 'wpcf-ending-date' , true);
    $ends= date('M j, Y', $ends);
    $waht_for = get_post_meta($post_id, 'wpcf-for' , true);
    $new_title = $starts.' - ' . $ends.' ('.$waht_for.')';
    
         
   
    $new_title = sanitize_text_field( $new_title );
    $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', 'swp_autogenerate_title',30,2);
 
        // update the post, which calls save_post again
        wp_update_post( $args );
 
        // re-hook this function
        add_action('save_post', 'swp_autogenerate_title',30,2);
   
  }  
}
add_action( 'save_post', 'swp_autogenerate_title', 30, 2 );

But still its giving warning on load that
Warning: date() expects parameter 2 to be int, string given
But working fine on publish.
Please help me to solve this.

#1565349

Minesh
Supporter

Languages: English (English )

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

I'm not sure why you are getting that notice. What if you disable the save_post action - do you still get the notice? If yes, I need a problem URL and access details to check things on your install.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1565357

Sorry we are not able to give any type of access.

By commenting below line its not giving any type of warning.
//add_action( 'save_post', 'swp_autogenerate_title', 30, 2 );

#1565375

Minesh
Supporter

Languages: English (English )

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

I'm afraid that without checking its even hard for me to guide you in the right direction.

I would like to know when you save your post, do you fill start and end date field values?

#1565405

I would like to know when you save your post, do you fill start and end date field values?
Yes.

Warning is not giving on publish or save post.

Warning is given on load (when click on add new).

#1565409

Minesh
Supporter

Languages: English (English )

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

I checked with my local test install and I do not see any warning when I try to add a new post which is having a custom date field created using Types plugin. Please check the screenshot: hidden link

Either you can send me access details or duplicator package with problem URL where I can see the issue or I'm afraid I can not do much here.

I have set the next reply to private which means only you and I have access to it.

#1565427

Error is giving due to this line
$starts= date('M j', $starts);

Default returning of date field is a timestamp right?
I am using this line for converting timestamp to readable format.

Have you tried this?

Or is there any other way to converting timestamp to readable format.

#1565481

Minesh
Supporter

Languages: English (English )

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

What if you try to change the code as given under and use it and try to resolve your issue:

function swp_autogenerate_title( $post_id, $post ){
       
   $starts = get_post_meta($post_id,'wpcf-starting-date',true);
   $ends = get_post_meta($post_id, 'wpcf-ending-date' , true);
   $waht_for = get_post_meta($post_id, 'wpcf-for' , true);

  if ( 'course-apply' == $post->post_type and  $starts!='' and  $ends!='' and  $waht_for!='' ) {
 
    $starts= date('M j', $starts);
    
    $ends= date('M j, Y', $ends);
   
    $new_title = $starts.' - ' . $ends.' ('.$waht_for.')';
     
          
    
    $new_title = sanitize_text_field( $new_title );
    $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', 'swp_autogenerate_title',30,2);
  
        // update the post, which calls save_post again
        wp_update_post( $args );
  
        // re-hook this function
        add_action('save_post', 'swp_autogenerate_title',30,2);
    
  }  
}
add_action( 'save_post', 'swp_autogenerate_title', 30, 2 );
#1567617

Hey, Minesh thank you so much. Now code is working perfectly.