1

I have a situation where I'm re building a tour transfer reservation site, I've created the reservations cpt's and each time a user place a reservations this automatically post as a new reservation.

Now I need a better way to locate each reservation with a unique number, another situation is that the previous system already had a sequential number so I will need this code to have an starting point, example:

previous system last number: 3000

new wp system starting point: 3001

new reservation: 3002

new reservation: 3003

and so on...

I have a vague idea on how to achieve this. Create a custom field for reservation code, each time a reservation is placed check for the last post reservation field and increment by one, but i don't know if this is the best way to do so.

4
  • I would have stored that value in a theme_setting, it's simpler in my opinion. More informations here
    – Sladix
    Jan 14, 2016 at 16:13
  • which one, the initial sequence number or the latest reservation to follow?
    – Al Rosado
    Jan 14, 2016 at 16:18
  • The sequence number and then you could increment it using a add_action('save_post')
    – Sladix
    Jan 14, 2016 at 16:25
  • Ok, I'm not really sure if I'll get it hehe, but I will give a try. If you could maybe give an example. Thanks.
    – Al Rosado
    Jan 14, 2016 at 17:11

1 Answer 1

0

One solution among others :

In a plugin, you create an option on it's activation in order to store your sequence number :

/*
Plugin Name: Reservation Increment
Description: test
*/


/**
*
* Name : myplugin_activate
* Description : Activation function
*
* @params
* @return 
*
**/

function myplugin_activate() {
    update_option('reservation_increment',3000);
}
register_activation_hook( __FILE__, 'myplugin_activate' );

Now you have to update that value and associate it with your new CPT when you create a new reservation :

/**
*
* Name : update_reservation_sequence
* Description : Updates the reservation unique incremential number
*
* @params
* @return 
*
**/
function update_reservation_sequence ($post_id, $post, $update){

    $slug = 'reservation_cpt';

    // If this isn't a 'reservation_cpt' post, don't update it.
    if ( $slug != $post->post_type  || $update) {
        return;
    }

    $increment = get_option('reservation_increment') +1;

    if ( ! add_post_meta( $post_id, 'reservation', $increment, true ) ) { 
       update_post_meta ( $post_id, 'reservation', $increment );
    }

    update_option('reservation_increment',$increment);

    return;
}

add_action( 'save_post', 'update_reservation_sequence',10,3 );

And then, to access it in your single-reservation.php template :

echo "<p>Reservation number : ".get_post_meta( get_the_ID(), 'reservation', true )."</p>";

Hope this will help !

1
  • It worked. Thank you very much. I was struggling trying to get this.
    – Al Rosado
    Jan 15, 2016 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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