0

I want to auto-generate post title like that Ex : ABC-123456 also i need to let (( ABC- )) fixed and random change the 06 numbers and to dont change the post title through updating the post

zozson
  • 3
  • 1
  • What is the trigger to generate the title? Saving the post from the admin? Other? – Tami Oct 15 '22 at 00:17
  • I'm using CPT UI and ACF to make shipments and I need to make the title as Shipment number to use it for tracking the shipment – zozson Oct 15 '22 at 03:48
  • OK, that I understand. However, you're still not saying what generates the cpt itself. Are you going to add them from the admin? Are they going to be generated from a WooCommerce order? What generates the cpt? – Tami Oct 15 '22 at 14:37
  • Yes I'm going to add them from admin – zozson Oct 15 '22 at 14:50
  • And they are a custom post type, right? What is the identifier that you used? – Tami Oct 15 '22 at 18:02
  • If you mean the theme I use blocksy – zozson Oct 15 '22 at 18:52
  • Or you mean identifier for title field, I don't use identifier with the field – zozson Oct 15 '22 at 19:14
  • Nop, I mean the CPT identifier. You said "I'm using CPT UI...", what is the slug/id of the CPT you configured, assuming this is the CPT you want this titles generated for – Tami Oct 15 '22 at 22:49
  • the slug is sho7nat – zozson Oct 16 '22 at 09:48
  • OK, I'm going to write a snippet for you, give me a few minutes – Tami Oct 16 '22 at 13:04

1 Answers1

0

First, to modify WordPress behavior the correct way, you find an appropriate hook. In this case, that would be a filter that allows changing the Post data before it is saved to the db.

The filter 'wp_insert_post_data' is exactly what you need, so you add your filter, and connect it to a function like so:

function zozson_filter_post_title(){
}
add_filter( 'wp_insert_post_data', 'zozson_filter_post_title',50,4);

'wp_insert_post_data' is the name of the filter

'zozson_filter_post_title' is the name you give to your function, to hook to it.

50 is the priority. I chose 50 to run it after most other things. Default is 10

4 is the number of variables that the filter passes to your function.

So now we will add those variables and the logic inside it, to assign these CPT sho7nat those titles on admin saving them.

function zozson_filter_post_title( $data, $postarr, $unsanitized_postarr, $update){

   //Then if it is the post type sho7nat
   if( $data['post_type'] !== 'sho7nat' ){
       return $data;
    }

    //If there is already a titled saved ($update returns true always)
   if( $data['post_title'] !== '' ){
       return $data;
    }

    //Let's build our title
    $post_title = ' ABC-';
   
    //What better random number that a unique timestamp?
    $random_number = strtotime('now');

    //Add the random number to the post title to save. You can do these in 1 line instead of 3
    $post_title.= $random_number;
   
    
     //We now have a post title with ABC- fixed and a random number, tell WordPress to use it as the post title
     $data['post_title'] = $post_title;

      return $data;

}
add_filter( 'wp_insert_post_data', 'zozson_filter_post_title',50,4);

The title automatically assigned should be like in this example: enter image description here

Tami
  • 686
  • 4
  • 8
  • first of all thanks a lot for your efforts, but I tried this code but not working I don't know why or where's the fault – zozson Oct 18 '22 at 12:07
  • Hi! Do you still need help? What does it mean exactly that it does not work, what is the saved titles? – Tami Oct 22 '22 at 15:04
  • yes Bro , when i tried to creat new shipment (( custom post )) i found the name still ( no title ) and i didnt found the name that i need – zozson Oct 23 '22 at 01:54
  • Hey, I am no bro, but reviewed the code, and now actually tested it, and I added a couple of adjustments to the answer above. It should now be working correctly, but if it does not, let me know. – Tami Oct 23 '22 at 13:22
  • If it works, please accept the answer to help others – Tami Oct 23 '22 at 13:25
  • Thanks alot Tami , It works but with a small problem the post title changed every time I update the post and I need it to be fixed because it's the shipment number , also the digits is too long 10 digits can you please make it only 6 digits? – zozson Oct 24 '22 at 01:34
  • I updated the answer to only save the title if it is empty. – Tami Oct 24 '22 at 11:47
  • The length of the number, I am sorry but I have a lot of work and won't have time to write a solution that gets a unique 6-digit number. Using timestamp, you assure the number will be unique. – Tami Oct 24 '22 at 11:48
  • Thanks Tami, it works fine now, Really I appreciate your efforts – zozson Oct 24 '22 at 13:19
  • Glad it helped, hopefully what goes around comes around :) – Tami Oct 24 '22 at 17:10