0

I'm using the Advanced Custom Fields - plugin and I have custom post type called 'course' where some fields from the ACF are related to.

I have this code below:

function update_course($course_id) {

    //Check conditions and tell user if certain values are incorrect/inconsitence
    $nr_available = intval(get_field('nr_available'));
    $nr_registered = intval(get_field('nr_registered'));

    echo 'nr avail=' . $nr_available;
    echo 'nr registered=' . $nr_registered;

}

add_action( 'edit_post' , 'update_course');

The thing I want to do is to check for instance if available spots are less then register = incorrect. Then I want to indicate that the user has done this incorrectly.

But anyway, my real issue is that I don't know how (I don't get it) to get values from ACF of the custom post type posted (course).

Above would only return the actual values in the database (for the current course - I think), I want the posted values. If I look at the $_POST - variable it shows something like:

[field_524bceba787b8] => 800 [field_524bcf0a787b9] => 40 [field_524bcf58787ba] => 50 [field_524bcfdb787bb] => [field_524bd005787bc] => [field_5253d4b31053b] => 0

UPDATE: Whole array looks like:

Array ( [_wpnonce] => 8fce853ce2 [_wp_http_referer] => /wp-admin/post.php?post=1911&action=edit&message=1 [user_ID] => 1 [action] => editpost [originalaction] => editpost [post_author] => 1 [post_type] => course [original_post_status] => publish [referredby] => http://{host}/wp-admin/post.php?post=1911&action=edit&message=1 [_wp_original_http_referer] => http://{host}/wp-admin/post.php?post=1911&action=edit&message=1 [post_ID] => 1911 [autosavenonce] => 0995694d61 [meta-box-order-nonce] => 1da8897fe1 [closedpostboxesnonce] => c92ea6dd81 [post_title] => Balett 8-9 år [samplepermalinknonce] => 31966e47cc [content] => coolt222 [wp-preview] => [hidden_post_status] => publish [post_status] => publish [hidden_post_password] => [hidden_post_visibility] => public [visibility] => public [post_password] => [mm] => 10 [jj] => 02 [aa] => 2013 [hh] => 07 [mn] => 52 [ss] => 46 [hidden_mm] => 10 [cur_mm] => 10 [hidden_jj] => 02 [cur_jj] => 08 [hidden_aa] => 2013 [cur_aa] => 2013 [hidden_hh] => 07 [cur_hh] => 20 [hidden_mn] => 52 [cur_mn] => 55 [original_publish] => Uppdatera [save] => Uppdatera [tax_input] => Array ( [categorycourses] => Array ( [0] => 0 [1] => 29 [2] => 27 ) ) [newcategorycourses] => Namn för ny kategori [newcategorycourses_parent] => -1 [_ajax_nonce-add-categorycourses] => 2a26873ebd [acf_nonce] => 52e8f939a6 [fields] => Array ( [field_524bcd27ed097] => [field_524bcde2787b4] => o [field_524bce12787b5] => 433 [field_524bce5f787b6] => 1/:00 [field_524bce98787b7] => [field_524bceba787b8] => 1000 [field_524bcf0a787b9] => 10000 [field_524bcf58787ba] => 5 [field_524bcfdb787bb] => [field_524bd005787bc] => [field_5253d4b31053b] => 0 ) [advanced_view] => 1 [add_comment_nonce] => 82940723b5 [_ajax_fetch_list_nonce] => 83bc001ed1 [post_name] => balett-8-9-ar [post_author_override] => 1 [post_mime_type] => [ID] => 1911 [post_content] => coolt222 [comment_status] => closed [ping_status] => closed ) 
1

You can find the Advanced Custom Fields fields which you added in the $_POST array under the 'fields' key. Use ACF's get_field_object() function to get the key of your field you added.

So if you named your field 'nr_available', you have to find its key to be able to find this field in the $_POST object (so you can fetch $_POST['fieldKey']).

Check the following code:

function update_course($course_id)
{
    $nr_available = 0;
    $nr_registered = 0;

    $field1 = get_field_object('nr_available');
    $nr_available_key = $field1['key'];

    $field2 = get_field_object('nr_registered');
    $nr_registered_key = $field2['key'];

    // loop the $_POST['fields'] which contains all the Advanced Custom Fields fields which you added to the post you are editing
    foreach ($_POST['fields'] as $k=>$v)
    {
        // $k are the custom fields keys
        // $v are the custom fields values

        if ($k == $nr_available_key){
            $nr_available = $v;
        }
        if ($k == $nr_registered_key){
            $nr_registered = $v;
        }
     }

     // Do your checks after you get the values
     if ( ($nr_available > $nr_registered) || $nr_available <= 0 )
     {
        // Bad
        return;
     }
}
add_action( 'edit_post' , 'update_course');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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