Skip Navigation

[Resolved] CRED API

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

This support ticket is created 7 years, 3 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by jasonw-2 7 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#468423
subm.jpg

I am trying to: add numbers from post fields in all posts from a post type

I have a post type Daily Log. Each employee inputs various information. Is it possible to query every submission from that post type for a specific field?

Meaning. If I had 10 employees submit a daily log and in those daily logs, each employee added a number (5). I would have a total of 50 for the week. What I'm trying to do is to query all the posts to automatically calculate that number and show it in a view with options of by the week and by the month.

Is this doable?

Thanks so much for any help!!

#468663

It's possible, but you'll require Custom PHP Scripts for that.

1. Create a Custom Field "Total"

2. On the CRED Form submission use a cred_save_data function, to grab the current submitted value of the Number Field and update the new Field "Total" with the sum of the already existing value and the newly submitted value

3. Display this Field in a View.

4. If you need to have several values, like monthly, weekly and daily, you need to create as many Fields as you need totals and sum the values to the already saved totals.

Eventually you will also need a script that deletes those values (as example with a CRON Event) every midnight, every sunday and ever last day of every month.

The CRED function can be found here:
https://toolset.com/documentation/user-guides/cred-api/#csd

You will need to use the WordPress and PHP API as well, to create your scripts:
hidden link
https://developer.wordpress.org/reference/functions/get_post_meta/
https://codex.wordpress.org/Function_Reference/update_post_meta

#468849

Hi Beda! Thank you for the help!

Do I have to group the new "total" custom field in with the fields of that particular post type? Or can it be not attached to any post type and I can populate / retrieve it using the cred_save_data function while it's there on it's own? (I hope that makes sense.)

Also... "You will need to use the WordPress and PHP API as well, to create your scripts:" Do I need those to make this work or are they only to set up the cron-type delete operations?

Thanks again!!

#468882

Let me explain the idea behind our, WordPress and PHP "API" (althout PHP is a framework, not an API 🙂 ).

CRED API is a set of hooks and filter, this means, there are a few functions that do things on certain moments of a CRED Form's "life".

As example the cred hook cred_save_data hooks into the moment when the post is saved to the database (right after).
The hook itself does nothing.
It just makes sure, that the code wich you put INTO that hook, is executed at the correct moment.

Now, all code that is IN the hook is what you need to build using PHP and the WordPress API.

The WordPress API will provide you with the correct functions, such as "update_post_meta", wich puts the correct information into a Custom Field in the Database.
The CRED API makes sure that the WordPress function gets executed at the correct moment.
And PHP will help you make the calculations, using the arithemtic oerators it offers (and much more)

Let's see the below very simple example, it updates a post meta, inside a CRED cred_save_data function, and uses the PHP operator "+" to sum a value to another value, before it updates the post meta.

//this adds the action at the right moment when we save stuff to the database with a CRED form
add_action('cred_save_data', 'my_save_data_action',10,2);

//This is the function that is executed, when above action is added. It acts on a specific CRED form only
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
        $value_one = 10; //either numerical values, or some value from a $_POST field, or some value from a get_post_meta field
       $value_two = 2; //same as above, you may add as many $variables as you need
       
       //now let's sum up the above two values
       $the_sum = $value_one + $value_two;

       //now let's update a Post Field with the above new value
       update_post_meta($post_id, 'wpcf-your-field-slug', $the_sum);
    }
}

The above code is built of the CRED API hook, that will make sure all code INSIDE The hook gets executed on form save to database, and only on this one form.
All code INSIDE The hook, is built with WordPress and PHP.

The field for your total sum, can be added to the currently edited or created post, or if it's on another post type, you have to specify the $post_id in your code above.
It depends on where you want the field to be appearing and used.

If you browse my pastebin you will see several example CRED functions, all built with above process:
http://pastebin.com/u/bedas

#470016

Hi Beda!

I can't thank you enough for trying to help me. While I understand the theory, my brain (I have onset Parkinson's) is having a hard to communicating the code to make this happen. It also doesn't really help that the holidays are here and there's too much going on to try to implement this, at this time. 🙁

Again.. thank you!!! I will eventually return to grab the info you've posted and will be successful, one way or the other! 🙂

Bless!! and good day. You do an amazing job and the entire Toolset community is lucky to have you!

Jason

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.