Skip Navigation

[Resolved] Auto incrementing number field

This support ticket is created 4 years, 10 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by JamesS2731 4 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#1459667

Tell us what you are trying to do? Add a permit number field that auto generates and auto increments for each permit (i.e. permit 1, permit 2, etc.)

Is there any documentation that you are following? I have been following your support posts at:
https://toolset.com/forums/topic/auto-numbering-posts/
https://toolset.com/forums/topic/disable-cpt-auto-numbering-when-editing-post/

Both of which were helpful. However, I can see an autogenerated number in the permit number field but it's always 1 in every permit. The code I'm using is as follows:

/* Auto number Permits */
add_action( 'save_post', 'auto_number', 100, 3 );
function auto_number( $post_id, $post, $update ) {
if ( $post->post_status == 'publish' && $post->post_type == 'permit' ) {
$project_args = array(
'numberposts' => 2,
'post_type' => 'permit',
'orderby' => 'post_date',
'order' => 'DESC'
);
$projects = get_posts( $permit_args );

// don't update existing auto-numbering
$this_id = get_post_meta( $permit[0]->ID, 'wpcf-permitnumber', true);
if ( $this_id ) {
return;
}

$last_id = get_post_meta( $permit[1]->ID, 'wpcf-permitnumber', true);

if ( !$last_id ) {
$last_id = 0;
}

$last_id++;
update_post_meta( $post_id, 'wpcf-permitnumber', $last_id );
}
}

permit is the post type and wpcf-permitnumber is the custom field.

The permits are being created with a front end post form.

Is there a similar example that we can see? Only in those posts.

What is the link to your site? hidden link

Any help much appreciated!

Kind regards
James

#1460033

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi James

I would use a different approach.

If the posts are being published exclusively using a front-end form then you can use the cred_save_data API hook to trigger your code, which runs once the form submission has been processed and the post data saved.

You can then run a quick custom query to find the highest existing value for your incrementing field, and then add post meta for this field to the new post with an incremented value.

You can use the following, edited for the ID of the form which publishes the posts:

/**
 * Add auto-incrementing custom field value
 */
add_action('cred_save_data', 'ts_auto_increment', 10, 2);
function ts_auto_increment($post_id, $form_data)
{

    if (in_array($form_data['id'], array( 123 ))) { // Edit form ID

        global $wpdb;
        $values = $wpdb->get_col("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'wpcf-permitnumber'");
        if ( is_array( $values ) ){
            $incremented = max( $values ) + 1;
        } else {
            $incremented = 1;
        }

        add_post_meta( $post_id, 'wpcf-permitnumber', $incremented, true );
    }
}
#1460095

Hi Nigel,

Many thanks for your quick response.

I've just tried using that code, with the form id replacing 123 (the post form id is 1261) as follows:-

add_action('cred_save_data', 'ts_auto_increment', 10, 2);
function ts_auto_increment($post_id, $form_data)
{

if (in_array($form_data['id'], array( 1261 ))) { // Edit form ID

global $wpdb;
$values = $wpdb->get_col("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'wpcf-permitnumber'");
$incremented = max( $values ) + 1;

add_post_meta( $post_id, 'wpcf-permitnumber', $incremented, true );
}
}

but it hasn't worked for me. I did wonder if the first permit wouldn't return a number as there was no previous post to look up, so I manually numbered the first permit and tried again with a second permit but, again, I just get a blank field.

I've double-checked custom field slug and form id. Anything else I need to check?

I look forward to your thoughts.

Kind regards
James

#1460105

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi James

I just edited my last reply to update the code to allow for there not yet being any posts published with a value for that custom field.

I then tested it (I hadn't before) and it worked.

I'm using add_post_meta rather than update_post_meta because I assume your form to publish the posts doesn't actually include the permitnumber custom field, it is purely being added by this code after the form is submitted, right?

#1460117

Many thanks Nigel, that's perfect. It's working now.

Thank you for your help with this!

Kind regards
James