Support

Account

Home Forums General Issues my_acf_save_post and conditional issues

Solved

my_acf_save_post and conditional issues

  • So I have a my_acf_save_post function that does a couple of things before it saves the post but it appears to be handling things strangely.

    <?php
    function my_acf_save_post( $post_id ) {
    
        
      // bail early if no ACF data
      if( empty($_POST['acf']) ) {
            
          return;
            
      }
    
      // bail early if editing in admin
      if( is_admin() ) {
        
        return;
        
      }
    
       //obtain the current page's post ID
      $project_id = get_the_ID();
            
      // array of field values
        $fields = $_POST['acf'];
    
      // specific field value
       $field = $_POST['acf']['assigned_project'];
       $field2 = $_POST['acf']['volunteer_role'];
    
      //check if person is wanting to be a teamleader, check if spot is filled
       if($field2 == 'Teamleader') {
        if (leaderCheck() == true) {
          header("Location: http://www.myurlpage.com");
          die();
        }
       }
    
      //check if project is full and bail/kick if true
      if(limitCheck() == true) {
    
        header("Location: http://www.myurlpage.com");
        die();
    
      }
      
       //update assigned field
       update_field('assigned_project', $project_id, $post_id);
    
        
    }
    
    // run before ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);
    ?>

    It works fine without the teamleader and limit check. But if I add them, when the user submits the form the page goes blank (rather than just reloading the page) and the post is still saved but just partially filled out.

    The functions leaderCheck and limitCheck both return true after checking a few things based on the get_posts array and are also saved in the plugins functions. Should I call these functions on the single.php page that I’m using the form on as well? Calling them in the function above doesn’t seem to do what I need them to.

    For more references here are the two other functions that are called in the above one:

    <?php
    function leaderCheck() {
      $teamLeaders = get_posts(array(
                    'post_type' => 'volunteer',
                    'role' => 'teamleader',
                    'meta_query' => array(
                      array(
                        'key' => 'assigned_project', // name of custom field
                        'value' => '"' . get_the_ID() . '"', // matches exaclty
                        'compare' => 'LIKE'
                      )
                    )
                  ));
    
      //store count
      $count = count($teamleaders);
    
       if ($count >= 1) {
    
        $filled = true;
      }
    
      return $filled;
    }
    ?>
    <?php
    function limitCheck() {
    
      //get the volunteers
      $volunteers = get_posts(array(
                    'post_type' => 'volunteer',
                    'meta_query' => array(
                      array(
                        'key' => 'assigned_project', // name of custom field
                        'value' => '"' . get_the_ID() . '"', // matches exaclty
                        'compare' => 'LIKE'
                      )
                    )
                  ));
    
      //store count
      $count = count($volunteers);
    
      //get project limit
      $projectLimit = get_custom_field('project_limit');
    
      if ($projectLimit >= $count) {
    
        $full = true;
      }
    
      return $full;
    }
    ?>
  • Not sure if this has anything to do with your blank screen, but getting the field values should look something like this

    
       $field = $_POST['acf']['field_5578ce8f3ccf1'];
       $field2 = $_POST['acf']['field_5578ce8f3ccf1'];
    

    You need to use the field keys for your fields instead of the field names.

    If you still get a blank screen turn on error reporting on the site so you can see why it’s dying.

    
    define('WP_DEBUG', true);
    define('WP_DEBUG_DISPLAY', true);
    
  • EDIT: Actually what ended up happening was as I was changing some varaibles I misspelled one of them.

    After looking over everything it seems that my get_posts is not returning anything in the array and is NULL.

    <?php
    $teamLeaders = get_posts(array(
                    'post_type' => 'mro_volunteer',
                    'meta_query' => array(
                      array(
                        'key' => 'assigned_project', // name of custom field
                        'value' => '"' . get_the_ID() . '"', // matches exaclty
                        'compare' => 'LIKE'
                      )
                    )
                  ));
    ?>

    This is the correct post type and correct key, I checked the volunteers that are published and they all have the project in question assigned to them through the relationship field. I’m not sure why it can’t query them. It’s limited to only one if that makes any difference.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘my_acf_save_post and conditional issues’ is closed to new replies.