Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter wizzy85

    (@wizzy85)

    Ok so why my function set_incremental_post_title with wp_insert_post_data filter isn’t ok?
    I could move the update immediately after the get in this way:

    function set_incremental_post_title( $data, $postarr ) {
        if( 'booking' == $_POST['post_type'] ) {
            if( ( 'publish' == $_POST['post_status'] ) && ( 'publish' != $_POST['original_post_status'] ) ) {
                $booking_num = get_field( 'inc_value', 'options' ) + 1;
                update_field( 'inc_value', $booking_num, 'options' );
                $data['post_title'] = 'BOOKING ' . $booking_num;
            }
        }
    
        return $data;
    }
    
    add_filter( 'wp_insert_post_data', 'set_incremental_post_title', 99, 2 );

    with if( ( 'publish' == $_POST['post_status'] ) && ( 'publish' != $_POST['original_post_status'] ) ) I check that the post is actually published and not updated or auto-saved…
    The thing I need now is just understand how stop the “saving process” if I reach the maximum bookings (that I already have).

    Thread Starter wizzy85

    (@wizzy85)

    Sorry bcworkz should I use wp_insert_post or save_post filter/action?
    I read the codex but I can’t understand which one is the last called just before the db insertion.
    I need it to check if I reached the maximum bookings for the tennis court too!

    Thread Starter wizzy85

    (@wizzy85)

    Thank you bcworkz for your long answer… I ended up with this solution:
    I completly hide the title box and I create the cpt title just before publishing the post for the first time (not on updates); here’s the code:

    function set_incremental_post_title( $data, $postarr ) {
        if( 'booking' == $_POST['post_type'] ) {
            if( ( 'publish' == $_POST['post_status'] ) && ( 'publish' != $_POST['original_post_status'] ) ) {
                $booking_num = get_field( 'inc_value', 'options' ) + 1;
                $data['post_title'] = 'BOOKING ' . $booking_num;
                update_field( 'inc_value', $booking_num, 'options' );
            }
        }
    
        return $data;
    }
    
    add_filter( 'wp_insert_post_data', 'set_incremental_post_title', 99, 2 );

    This seems to work pretty well.
    I’m just asking myself what happen if two or more people will publish a booking at the same time…
    I know that it could be near impossible but without a “random” string in my title I should end up to have two or more booking with the same title… am I wrong? There is something I could do to avoid this?

    Thread Starter wizzy85

    (@wizzy85)

    Ok! Here the working code:

    function change_mycpt_default_title( $post_title, $post ) {
        if( 'booking' == $_GET['post_type'] ) {
            $post_title = 'My custom code ' . $post->ID;
        }
    
        return $post_title;
    }
    
    add_filter( 'default_title', 'change_mycpt_default_title', 10, 2 );

    Thread Starter wizzy85

    (@wizzy85)

    OK thank you very much, I hope you will resolve this problem, anyway at the moment your solution is enough.

Viewing 5 replies - 1 through 5 (of 5 total)