Support

Account

Home Forums General Issues Comparing get_fields() with $_post['acf'] orders are flipped

Solved

Comparing get_fields() with $_post['acf'] orders are flipped

  • Hi, I had this amazing code that would compare the changes made to a specific post type and than send an email that compared the old and new post and lists the changes.

    Now I had to change some fields and move them around which resulted in the order of the get_fields() to be completely different than the $_post['acf'] which was the same before I made the changes to the custom fields. What would be the reason for the get_fiels() to completely change its order and not be remotely logical.

    Is there an easier way to compare the two data sets to always work what ever the changes I make or do I have to remove all the fields and make them again to have the order be correct?

  • get_fields() returns fields in the order that they were created. get_fields() uses get_post_meta() to get the fields. There isn’t a way to order what is returned from WP when using this function.

    The order of $_POST[‘acf’] is the order that the fields appear in the form. There isn’t a way to set this order either as it is done by the browser.

    The way to check this is to loop through each $_POST[‘acf’] value and use the field key to get the old value instead of trying to get all of the fields at once and depending on them being in the same order, something like this.

    
    if (!empty($_POST['acf'])) {
      foreach ($_POST['acf'] as $field_key => $new_value) {
        $old_value = get_field($field_key, $post_id);
        if ($new_value != $old_value) {
          // do something
        }
      }
    }
    
  • Thank you for your reply.

    Because I wanted some sort of custom order and not have it completely random, I created this function

    https://gist.github.com/mvaneijgen/f169750f01f3677ccda34d12dd89dc93

    I would just like it to share and have this issue resolved. What I did was create a list of fields I wanted to see in the email (not all the fields are relevant) and have them in the order seen in the array $arrayKeysOldItems next I compared the $_post['acf'] array stored in $arrayKeysNewItems and had them ordered the same as the previous array. Than I just looped through all the items in the first array and compared them with the values of the new array and if they were different I add them to the email.

    In my final code I also do some converting of sting to date and time stamps because not all the values are in the same way stored, but that would just clutter up this code.

    Thanks again for your feedback! Seems like a much easier solution!

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

The topic ‘Comparing get_fields() with $_post['acf'] orders are flipped’ is closed to new replies.