Home Forums Add-ons Flexible Content Field Update number field by adding new CPT action


Update number field by adding new CPT action

  • Hello everyone,

    I’m developing a WordPress plugin and I’m stuck on something.

    I created two custom post types, the first one is “Clients” and the second one is “Actions clients”.

    In the “Clients” CPT, I added a ACF number field to store the number of available credits. In the “Actions clients” CPT, I list different actions with credits assigned to each of them.

    By adding a new client action, I want update the total of available credits in the first CPT. Like $totalCredits = $totalCredits – $actionCredits.

    I tried different codes :

        public function update_data_acf($post_id) {
            $field_key = get_field('credits_disponibles');// credits available
            // credits_available - credits number (of an action)
            $value = get_field('credits_disponibles') - get_field('nombre_de_credits');
            update_field( 'credits_disponibles', $value, $post_id );
            return $field_key;

    Or like this :

        public function update_credits_acf( $post_id ) {
        $post_type = get_post_type( $post_id );
        $post_status = get_post_status( $post_id );
        if ( $post_type == 'npluginclients' ) {
            $args = array( 'post_type' => 'npluginclients');
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
                    $number_of_credits_available = get_field('credits_disponibles', the_ID());
                $number_of_credits = count(get_field('nombre_de_credits', the_ID()));
                $number_of_credits_left = $number_of_credits_available - $number_of_credits;
                update_field('credits_disponibles', $number_of_credits_left, the_ID());

    Then I’m using this (Boiler Plate Plugin Generator ):

    $this->loader->add_action( 'acf/save_post', $plugin_admin, 'update_credits_acf' );

    I’m probably making a lot of big mistakes, but I’m new in developing. Thanks in advance !

  • In your first example you are attempting to get fields from 2 different posts without providing the post ID to ACF.

    In the second example you need to add

    global $post;

    for your post loop to work.

  • Thanks for your answer John.
    I’m updating this post because I find a way to accomplish my goal :

    public function update_credits_acf( $post_id ) {
    $post_id = 137; // post_id of my CPT post
    $number_of_credits_available = get_field('credits_available', $post_id);
    $number_of_credits_action = get_field('number_of_credits');
    $number_of_credits_left = $number_of_credits_available - $number_of_credits_action;
    update_field('credits_available', $number_of_credits_left, $post_id);

    This code works, now my question is : How to dynamically retrieve the id of my CPT ? I have several posts so I can’t let “$post_id = 137;”.

    I tried some actions with get_the_ID(), the_ID()…

  • Is there a field on the post being saved that relates it to the post on the other CPT?

  • Get the unformatted value of that field…. but do not alter $post_id

    $other_post_id = get_field('relationship_field_name', $post_id);
    // get and update fields from other post using $other_post_id
    // get and update fields on this post using $post_id
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.