Skip Navigation

[Resolved] How to set Post ID as custom field value

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to save post Id as unique custom field value

Solution:
You can use CRED hook "cred_save_data" to set the custom field value as post ID.

You can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/increment-a-custom-field/#post-630990

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 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.

Our next available supporter will start replying to tickets in about 0.44 hours from now. Thank you for your understanding.

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 pamb-2 6 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#630678

I am trying to: auto-increment a field named COUNT in my CPT Issues
I'm using this function from this solution from a resolved thread:
https://toolset.com/forums/topic/increment-value-of-custom-field-with-cred/

add_action('cred_save_data', 'increase_custom_value',10,2);
function increase_custom_value($post_id, $form_data)
{
   
    //NOTE: Set your form ID instead of 12
    if ($form_data['id']==2775)
    {
        //Get count field from database and add 1
        $count = intval(get_post_meta( $post_id, 'wpcf-count', TRUE )) +1;
   
        //Update count field
        update_post_meta($post_id, 'wpcf-count', $count);
    }
}

With a CRED form id=2775. I have ajax DISabled.
The field COUNT is numeric.
I have disabled all plugins but Toolset:
CRED
Types
Views
MAPS
Using the generic theme that comes with WordPress 2017

I just want every new record to get a value in "COUNT" that is the value of COUNT of the last record + 1.
That isn't happen though, the field keeps getting updated with the same number, "1".

#630811

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - it looks like you want to have your CPT Issues global count so if new entry added it will count existing entries + 1 - correct?

#630922

Yes, that's correct.

#630933

Minesh
Supporter

Languages: English (English )

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

Well - you should use global setting field to save total count for your CPT issue as you do not need to attach global count with every post.

For example:


dd_action('cred_save_data', 'increase_custom_value',10,2);
function increase_custom_value($post_id, $form_data)
{
    
    if ($form_data['id']==2775){

$option_name = 'cpt_Issues_count' ;
if ( get_option( $option_name ) !== false ) {
    $total_count = get_option($option_name) + 1;
    update_option( $option_name, $total_count );
} else {
   $new_value = 1;
   add_option( $option_name, $new_value, null, 'no' );
}

    
    }
}

Could you please check and try to resolve your issue with above code.

#630945

Should I replace $option_name with the field name, "count" or "wpcf-count" ?

#630946

Minesh
Supporter

Languages: English (English )

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

No - you should keep the same name - so you will know it's global count field for CPT issue.

so - you need to deal with option name cpt_issues_count .

Please use following code - as I've made some adjustment to code:

add_action('cred_save_data', 'increase_custom_value',10,2);
function increase_custom_value($post_id, $form_data)
{
     
    if ($form_data['id']==2775){
 
$option_name = 'cpt_issues_count' ;
if ( get_option( $option_name ) !== false ) {
    $total_count = get_option($option_name) + 1;
    update_option( $option_name, $total_count );
} else {
   $new_value = 1;
   add_option( $option_name, $new_value, null, 'no' );
}
 
     
    }
}
#630948

That didn't work. The field "count" did not get updated at all.

#630949

Minesh
Supporter

Languages: English (English )

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

Where you are displaying the count?

The code I shared will work from start - if you need existing counts - it needs to be adjusted.

I need access details and where you want to display the count?

*** 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

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

#630957

Minesh
Supporter

Languages: English (English )

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

Please check following screenshot:
=> hidden link

I've find the option key "cpt_issues_count" in database table options and adjusted the current number of counts value and then I've added the new record and I see the code is working fine.

Currently you have 7 entries now.

#630969

Minesh
Supporter

Languages: English (English )

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

BTW - if you want to have unique post title - you should just attach post ID to your title to make it unique.

#630971

I think there's a miscommunication of what I am trying to achieve.
I want to be able to update the record with a unique number, then display this unique number.

I have a field named "count" for this purpose. On the "add issue" form SAVE, I want that number to be assigned, or updated.

It should display in this field in the back end: hidden link

It should display when you display the record itself: hidden link

Your test left the field "count" empty, instead of filling it with a unique ID.

#630975

Minesh
Supporter

Languages: English (English )

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

I've updated the code as given under to generate the unique ID:

add_action('cred_save_data', 'increase_custom_value',10,2);
function increase_custom_value($post_id, $form_data)
{
      
    if ($form_data['id']==2775){
	  
	  $unique_id = abs( crc32( uniqid() ) );    
        //Update count field
        update_post_meta($post_id, 'wpcf-count', $unique_id);
    }
}

And I've created this test entry and I can see the tracking ID is displayed:
=> hidden link

#630989

Getting closer, great! That number is a bit too long, what if I wanted:

$unique_id = the new current post id

Is that possible?

#630990

Minesh
Supporter

Languages: English (English )

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

Yes - that is what I already suggested here: https://toolset.com/forums/topic/increment-a-custom-field/#post-630969

I again updated the code as given under:

add_action('cred_save_data', 'increase_custom_value',10,2);
function increase_custom_value($post_id, $form_data)
{
      
    if ($form_data['id']==2775){
	  
	  $unique_id = $post_id;    
        //Update count field
        update_post_meta($post_id, 'wpcf-count', $unique_id);
    }
}

I hope you get it what you want now. You should create a new post and check.

#630992

Thank you so much for staying with me "live" as we worked through this! That's a great solution.

In the future, I'd recommend as a feature, a way to have an auto-incrementing field that is sequential, since post id is not always sequential.

Thanks!