Support

Account

Home Forums Add-ons Repeater Field get_field occassionally returning string with repeater field

Solved

get_field occassionally returning string with repeater field

  • Hello & good day!

    Our WordPress website (with the ACF plugins & a custom theme using these) is suffering some problems with the get_field() function – both with ACF repeater fields & just using get_field() in other parts of the custom theme.

    We have 3 of these repeater fields displayed on every page of our website (you can see for yourselves at uniqueteambuilding.com.au with the titles “As Seen On”, “Our Happy Clients”, “Testimonials” at the footer). And get_field() is also used for thumbnails throughout the site.

    From my investigations either all 3 of the repeater fields work and the thumbnails don’t, or all 3 of the repeater fields display empty content and the thumbnails display fine. This changes as you switch page in the site or refresh the page.

    When the galleries display empty content it is because the get_field() function returns a string, i.e. ‘string(1) “6”‘, instead of the expected array.

    The thumbnails not displaying may be another completely different issue, I still have more to go with that investigation. But it must be related & relevent in the sense that when these works, the repeater fields don’t. It appears (according to What The File plugin) that the relevant PHP template part file is only used when the thumbnails do work, i.e. when that appropriate file is being used and the get_field() function is called, then the get_field() function for the repeater fields returns that erroneous string.

    Below is the code being used for the repeater fields;

    <h3 class=”text-center”><?php the_field(‘as_seen_on_block_title’, “options”) ?></h3>
    <?php if ($logos = get_field(“as_seen_on_logos”, “options”)): ?>
    <ul class=”slider-as-seen-on”>
    <?php foreach ($logos as $logo) {…

    <h3 class=”text-center”><?php the_field(‘logo_block_title’, “options”) ?></h3>
    <?php if ($logos = get_field(“logos”, “options”)): ?>
    <ul class=”logos”>
    <?php foreach ($logos as $logo) {…

    <?php if ($testimonials = get_field(“testimonial_list”, “options”)): ?>
    <ul id=”testimonials” class=”bxslider”>
    <?php foreach ($testimonials as $testimonial) {…

    & here is the code used for the thumbnails;

    <?php $image = wp_get_attachment_image_src(get_field(‘event_thumbnail’), ‘full’); ?>
    ” alt=”<?php echo get_the_title(get_field(‘event_thumbnail’)) ?>” />

    Any advice or help fixing this would be amazing!!

    Thanks very much!
    Aidan

  • First I am going to repeat my comment from your other topic the if statements here are not effective and the code inside of them will run whether or not get_field() returns any results.

    Second issue is that you are now taking about repeaters that are completely different than a gallery field.

    When you use get_field(‘repeater’) and you get a string back, the issue is that ACF is returning the actual value of the repeater from the database and this value is the number of rows contained in the repeater. When this happens it is due to a conflict with some other code on the site and this is usually cause by a “pre_get_post” filter that is interfering with ACF getting values. Since this is an options page it could also be any filter in WP getting options.

    The first thing that you need to do is figure out where this conflict is. You need to test by deactivating other plugins and possibly switching themes to narrow down the cause. Once the cause is narrowed to the theme or a specific plugin it will be easier to figure out.

  • Hi John,

    Thanks very much! Yes, apologies for the confusion – none of the website, the custom theme, or this code was done by myself, so I am still learning as I go along.
    But yes I believe it is repeater fields I am talking about and not galleries.

    Yes I have tried disabling all other plugins, and the error still persists so this must be the custom theme causing this.
    These repeater fields are part of the custom theme so I haven’t tried changing theme. But I’m sure it is the custom theme causing this, namely the code to get thumbnails doing this (as I mentioned in the post) as when that code isn’t used the repeater fields work fine. Again, see the post for the actual code for getting the thumbnails.

    Do you have any idea of beginning to figure this out (now that we know what code is causing it)?

    Thanks very much!

  • Sorry for the late reply, it seems I am not getting email notification from the forum for some reason.

    You need to look at filters on the site in the custom theme, search code for add_filter or add_action calls. I would start by looking for filters on the hook pre_get_posts an you can search for that as well. These are usually the cause of this problem. At the top of these filters make sure that the code is testing to see if the query is the main query before it alters the query.

  • Hi John, ok thanks very much for that!

    Apologies for the much later reply, but I have found some of the code that you mentioned. In the file used for the thumbnails there is the following code;

      add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
      function add_my_post_types_to_query( $query ) {
        $query->set( 'post_type', array( 'post', 'event' ) );
        return $query;
      }

    So now this is finally all making sense – this is the main problem here (whenever this file is being used and this code is executed, the repeater fields failed to load as described). And whenever this file is used, the thumbnails work and the repeat fields don’t. Thanks very much for helping me find this!

    Now can I ask if you have any idea on how to fix this? As I say I didn’t create this theme so I’m not sure on how it works, so your help would be very much appreciated!

    Thanks,
    Aidan

  • 
    add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
      function add_my_post_types_to_query( $query ) {
        // test to make sure this is the main query
        // and not the admin
        if (is_admin() || !$query->is_main_query()) {
          return;
        }
        $query->set( 'post_type', array( 'post', 'event' ) );
        return $query;
      }
    
  • Hi John,

    Thanks for that & apologies for the delayed reply. I have tried this solution – it does allow the repeater fields to populate ok, but at the expense of the thumbnails not appearing and the relevant code not running.

    I have found this is to do with $query.

    Below is the relevant code, echo tests were done throughout

    add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
      function add_my_post_types_to_query( $query ) {
        // test to make sure this is the main query
        // and not the admin
        if (is_admin() || !$query->is_main_query()) {
          return;
        }
        $query->set( 'post_type', array( 'post', 'event' ) );
        return $query;
      }
    
      the_post();
      $url = $_SERVER['REQUEST_URI'];
      $url = explode("/", $url);
      $field = $url[count($url)-3];
      $value = $url[count($url)-2];
      $query = new WP_Query( array(
        'numberposts' => -1,
        'category_name' => $value
      ));
    ?>
    
    <?php echo 'text1'; ?>
    <div class="row">
      <?php echo 'text2'; ?>
      <div class="col-md-9">
        <?php echo 'text3'; ?>
        <?php get_template_part('templates/page', 'header'); ?>
        <?php the_content(); ?>
        <?php echo 'text4'; ?>
        <?php while ($query->have_posts()) : $query->the_post(); ?>
          <?php echo 'text5'; ?>
          <article <?php body_class("event") ?>>
            <div class="well">
              <div class="container">
                <div class="row">
                  <div class="col-md-4">
                    <?php echo 'text6'; ?>
                    <div class="img-thumbnail">........

    And on the site, the echo call for ‘text5’ is not met, only up to ‘text4’. So I guess this is because <?php while ($query->have_posts()) : is never met because if (is_admin() || !$query->is_main_query()) is true?

    Do you know how to proceed with this? As I say this is way out of my comfort zone, so your help is really appreciated!

    Many thanks,
    Aidan

  • If you are doing a query in the template you should be applying those arguments $query->set( 'post_type', array( 'post', 'event' ) );
    directly in your new query and not using a pre_get_posts query. The only way that using this filter us useful is when adjusting the main query.

  • I’m afraid John I don’t understand what this means, I don’t understand anything about queries / $query – this template is from a third party.

    All I do know is that adding

    if (is_admin() || !$query->is_main_query()) {
         return;
     }

    to the add_action... code caused this…

    Would you be able to explain what you’ve said in a few more steps please?

  • Because of the way you are doing the query there is no reason to have a pre_get_posts filter. You should eliminate your filter completely and alter the query directly

    
    
      $url = $_SERVER['REQUEST_URI'];
      $url = explode("/", $url);
      $field = $url[count($url)-3];
      $value = $url[count($url)-2];
      $query = new WP_Query( array(
        'post_type' => array( 'post', 'event' ),
        'numberposts' => -1,
        'category_name' => $value
      ));
    
  • Hi John,

    Thank you, I have tried that and this does help a little – now the file is loaded in and the repeater fields appear correctly. Thank you!

    To be clear what I have tried is replacing the $url = $_SERVER... code block with the one you provided, and also deleting the pre_get_posts code block (this step makes no major difference).

    However, the issue of the thumbnails loading in is still present, unfortunately.

    The relevant code for this is;

    <?php echo 'text6'; ?>
                    <div class="img-thumbnail">
    				    <a href="<?php the_permalink() ?>">
    					    <div class="price-per-person">
    						    <span>FROM <br/><strong><?php echo get_field("price_from") ? "$" . get_field("price_from") : "$" . get_field("price_per_person") ?></strong> <br/>pp</span>
    					    </div>
                            <?php $image = wp_get_attachment_image_src(get_field('event_thumbnail'), 'full'); ?>
                            <img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_field('event_thumbnail')) ?>" />
          					        <?php
        				            ?>
    				    </a>
    				    <?php echo 'text7'; ?>
                    </div>
                  </div>

    All the text (the titles & price per person etc) loads in fine, as does all the textX tests, however there is still no image or thumbnail.

    Many thanks

  • Update: my original problem has been solved with John’s help, thanks very much! Because of this, this problem has obviously been solved. I will continue on with this thumbnail problem myself and possibly another support topic. Thank you!

  • Update II: In response to the issue of my thumbnails not displaying; I have figured this out if anyone else with similar issues stumbles into this…

    Basically, for me, I eliminated the then unnecessary pre_get_posts filter and then replaced;

    <?php $image = wp_get_attachment_image_src(get_field('event_thumbnail'), 'full'); ?>
    <img src="<?php echo get_field('event_thumbnail'); ?>" alt="<?php echo get_the_title(get_field('event_thumbnail')) ?>" />

    with:

    <?php $image = get_field('event_thumbnail'); ?>
    <img src="<?php echo $image; ?>" alt="<?php echo get_the_title(get_field('event_thumbnail')) ?>" />

    For some reason (probably some conflict again), wp_get_attachment_image_src returns as false. But get_field('event_thumbnail') returns the image anyway, so all that was needed was to remove this wp_get_attachment_image_src function!

    Hope this helps someone!

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

You must be logged in to reply to this topic.