1

At first I thought it was an ACF problem, but printing some values helped me to find out that somehow my Loop seems stuck on a single post, but only when some fields are concerned.

To sum up my setup: I have installed the Advanced Custom Fields plugin and defined some custom fields (mostly numerical ones), on which I built custom post types.

The first occurence of the problem apperead while I was developing an archive page for a custom post type: I got everything working but a few fields returned always the same values, those related to a specific post (post number 50). After some attempts to fix the code (which led to this question) I left the archive page behind and started working on the single page. Then, again, most of the fields worked fine, but a few (the same few of the archive page) kept returning always the same values (again, the values from post number 50). So I started printing out the post ID, and every time I call get_the_ID() I always get 50, even after I deleted said post, in a pretty useless attempt to get rid of its values.

I think it's a bit strange, but from the behaviour I'd say that the loop is somehow stuck on the same post, no matter where I call it.

  • what exactly wants to do? – Vasim Shaikh Feb 1 '16 at 17:57
  • I want to print out my custom fields (along with the rest of the post) – frollo Feb 1 '16 at 18:05
  • this is first post ID -50 – Vasim Shaikh Feb 1 '16 at 18:13
  • Do you mean the default first post? – frollo Feb 1 '16 at 18:19
  • yes...id 50 is which post – Vasim Shaikh Feb 1 '16 at 18:22
0

I got it! For any one in the future with the same problem:

ACF's get_field_object() apparently makes a WP_Query and doesn't clean up after so, since I called it for every post, I always got the same post after a certain point in the elaboration. I found the solution in this question.

Some code for clarity:

<?php while ( $query->have_posts() ) : $query->the_post();?>
 <h1><?php the_title();?></h1>
 //Some more prints
 <?php $field = get_field_object('my_field');
   $value = intval($field["value"]);
   $label = $field["choices"][$value];
   echo $label;
  //From this point on every custom field or other data came from the same post
 ?>

There are probably better ways to fix this, but it is simple and it works fine:

<?php while ( $query->have_posts() ) : $query->the_post();?>
 <h1><?php the_title();?></h1>
 //Some more prints
 <?php 
   $backup = $post; //Backing up the current post
   $field = get_field_object('my_field');
   $value = intval($field["value"]);
   $label = $field["choices"][$value];
   echo $label;
   $post = $backup; //Restoring the post
 ?>
| improve this answer | |
0

Have you tried using wp_reset_query() before the loop.

| improve this answer | |
  • Yes, it didn't work – frollo Feb 2 '16 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.