Support

Account

Home Forums General Issues Passing all ACF form data to a React table

Solved

Passing all ACF form data to a React table

  • Can someone clarify how to get the information stored by ACF (I’m using v4) on individual posts from the database? This question (Database structure) seems to suggest it’s possible but I’m not familiar enough with WP to understand how.

    I need to collect the data on every post generated by ACF (in the custom fields on the back end), and pass it to a small app I’ve made in react.

    Thanks for the help!

  • It really depends on what type of fields you’re using.

    Basic fields (anything listed under “Basic” for field type) in ACF store text information, beyond that it gets complicated. To be honest, I don’t even know what’s in the database for all field types.

    The “meta_key” value to use can get complicated if you’re using repeater or flex fields.

    It would be a very long post to go into every type of field. If you can give more information about exactly what kind of fields you’re dealing with that would make it easier. In most cases the way I’ve figured it out was to open up phpMyAdmin and dig around to figure it out.

  • @john Thanks for trying to help! I know it was a bit of a crazy question. Actually I think I (mostly) solved the problem. It’s kind of a hack because I can’t rely on using something like the WP API to GET my data, but my team agrees it’s ok for now.

    The only issue I’m having at the moment related to this is with the date picker field. I need to display a “Date Completed” column in a table. Right now, if I don’t supply a value, it defaults to Jan 18 2017. So even if there’s nothing in the field (I haven’t selected a date), it’s still acting as if a date was supplied.

    At this point, I don’t know if you’d prefer I switch topics (or open a support ticket) though as this may be a separate/off-topic problem. Thanks again!

    Edit –

    My temporary work around is just a quick check on the status field like this…

    
    if ($request_status !== 'Complete') {
      $formatted_date = ''; // the variable I'm passing to my array.
    } else {
      $date = get_field('date_completed', $post_id);
      $date_completed = new DateTime($date);
      $formatted_date = $date_completed->format('M j Y');
    }
    // continue...
    
  • If a value has not been set then ACF will return NULL for the value and I don’t know what effect passing null to new DateTime() has. I’ve personally never used this class for dates, but that NULL value is probably what’s causing the wrong date value.

    something like this would do the same thing and I would not consider it hack. It’s the way that ACF works.

    
    $formatted_date = '';
    if (get_field('date_completed', $post_id) {
      $date = get_field('date_completed', $post_id);
      $date_completed = new DateTime($date);
      $formatted_date = $date_completed->format('M j Y');
    }
    
  • @hube2 Thanks for the help! Here’s what I found/did. I’ll double check your code, (because I like it better than mine). In general I’d say it would work. However (not to be argumentative because I really appreciate the help) get_field('time_picker_name', $post_id) will always return true.

    This is because date picker automatically selects the current day (whatever ‘today’ is) even if no other value has been selected. So for instance on an apparently blank field it’ll return today’s date. When a date is selected afterwards, it changes to that date and explicitly populates the field.

    As I said I prefer your code though because it doesn’t rely on checking against a different field like mine does. I needed to do this…

    
    $request_status = get_field('request_status', $post_id);
    
    if ($request_status !== 'Complete') {
      $formatted_date = '';
    } else {
      $date_complete = get_field('date_complete', $post_id);
      $date_completed = new DateTime($raw_date);
      $formatted_date = $date_completed->format('M j Y');
    }
    

    But, I don’t like that I have to rely on my user explicitly setting ‘Complete’ as an option for the select field. I’ll continue to research/work on this.

  • instead of get_field('time_picker_name', $post_id) try get_field('time_picker_name', $post_id, false)

    The third argument tells ACF not to format the value. To be honest, I’ve never used a date field in ACF as much as I’ve used ACF. But not formatting the value should work I think.

    
    $formatted_date = '';
    if (get_field('date_completed', $post_id, false) {
      $date = get_field('date_completed', $post_id);
      $date_completed = new DateTime($date);
      $formatted_date = $date_completed->format('M j Y');
    }
    
  • @hube2 That’s exactly what I needed. Thanks very much!

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

The topic ‘Passing all ACF form data to a React table’ is closed to new replies.