Support

Account

Home Forums General Issues get_field not returning value

Solved

get_field not returning value

  • I’m trying to customize my Divi theme and cannot get get_field to return the value of an ACF field from one of my posts. After trying many variations in a child theme, I decided to modify the main theme code (after backing up, of course) in case the child theme was causing issues and have made the following modifications to page.php:

     <?php while ( have_posts() ) : the_post(); ?>;
      <?php	// skip expired posts
             $expirationtime = get_field("expiration");
             if ($expirationtime) {
               $secondsbetween = strtotime($expirationtime)-time();
               if ($secondsbetween <= 0) continue;
    	  }
      ?>;

    The expiration ACF field of the first post has a value of “04/10/2018”, the following two posts do not have values for the expiration field.

    get_field in this context, I believe, should get the value of expiration for the current post in the loop, but every combination I’ve tried, for example; get_field(“expiration”, $post->ID) or get_field(“expiration”, the_ID()), results in the expired post appearing along with the others. Setting $expirationtime to a constant value (eg “04/10/2018”) results in none of them displaying, as expected. So I’m certain it’s the get_field call that’s failing.

    I’m at a complete loss — any ideas/suggestions would be greatly appreciated.

  • Are you sure that the field is returning nothing or could stringtotime() not be evaluating the returned value correctly?

    
    <?php while ( have_posts() ) : the_post(); ?>;
      <?php	// skip expired posts
             $expirationtime = get_field("expiration");
             // try outputting the values
             echo $expirationtime,': ',strtotime($expirationtime);
             if ($expirationtime) {
               $secondsbetween = strtotime($expirationtime)-time();
               if ($secondsbetween <= 0) continue;
    	  }
      ?>;
    
  • Thanks for the suggestion, John. I modified your code slightly:

    $expirationtime = get_field("expiration");
     echo "<div>Expiration date: " . $expirationtime . "</div>";

    and this is the result:
    <div>Expiration date: </div>

  • You said that you tried

    
    $expirationtime = get_field("expiration", $post->ID);
    

    If that does not work then there are only a few explanations for why ACF is returning no value.
    1) The field does not exist on the post
    2) The field has no value
    3) The field name you’re trying to get is not the correct one
    4) Some other filter somewhere is interfering with ACF getting the field and formatting the value. This last one can be difficult to track down.

  • I’ve verified #1-3: the field exists on the post, it has a value, and I’m trying to get the correct field name. And #4 is going to be a PITA.

    I upgraded to version 5 to see if that might help. It fixed some minor problems (eg edit fields appearance, location) but it had no effect on filtering expired posts.

    I’ve disabled my other plugins (Duplicate Page, MINDBODY Widget, Monarch Plugin, WP Super Cache) to no effect.

    Don’t know what to try next. Any thoughts about what might be interfering? Are there error log files I can search – maybe get_field() is throwing an error or not being found?

    PS. Appreciate your help, and I’m still Googling around for ideas and things to try.

  • I don’t think this is an ACF issue anymore. I tried this standard WP call:
    $expirationtime = get_post_meta($post->ID, 'expiration', true);
    and got the same results – no value returned.

    I think Divi is probably the culprit, so I’ll see if their support can shed any light on the problem.

    Thanks again for looking at this, John.

    EDIT: I have it working now, and for anyone interested in the solution, the check for the expiry field needs to be in Divi\includes\builder\module\blog.php (not in page.php)

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

The topic ‘get_field not returning value’ is closed to new replies.