Yes, that makes sense. Thank you for your help!
Ah yes that worked! I was just playing around with it and it seems like the following works too. Are they both valid ways of writing the code, or is one better syntax than the other?
<?php
// get only first 1 results
$ids = get_field('testimonials_relationship', false, false);
$the_query = new WP_Query(array(
'post_type' => 'testimonials',
'posts_per_page' => 1,
'post__in' => $ids,
'post_status' => 'any',
'orderby' => 'rand',
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
// Must be inside a loop.
// If there are testimonials chosen display:
if ($ids) { ?>
<h2><?php the_title(); ?></h2>
<?php } else { ?>
<!-- No testimonials for this page -->
<?php } ?>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Hmm, that just breaks the page. This syntax doesn’t seem correct. I don’t know enough PHP to troubleshoot!
Ah, turns out I just need to remove the If and While statements. Nevermind!
Can anyone help with this please?
My site is on ACF 4.3.9 (the free version), not using PRO. Is that the problem? I just replaced all the ACF and Repeater files and it’s still not working.
I’m also having this problem. Just updated to the most recent ACF and still can’t update an image in a repeater field.
Hi! Just wanted to update you on this. I had a programmer look at it and apparently the issue was that the $posts global variable was being overridden.
When I searched through the code, the only places I saw that variable mentioned was in the main WordPress files, the ACF plugin, and the Event Calendar plugin (which I tried disabling and didn’t make a difference). So I’m not sure exactly what was the culprit there, but once that variable name was changed, it worked fine.
Here’s the code that ended up working in case anyone else has a similar issue:
<?php $store_type_posts = (array) get_field('store-type'); ?>
<?php foreach ($store_type_posts as $store_type_post): ?>
<?php $store_type_logo_id = get_field('store-type-logo', $store_type_post->ID) ?>
<?php if ($store_type_logo_id) : ?>
<?php $image = wp_get_attachment_image_src($store_type_logo_id, 'Store_Logo'); ?>
<?php $alt_text = get_the_title($store_type_logo_id); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo $alt_text; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" class="plain" />
<?php endif ?>
<?php endforeach ?>
Thanks for your help Elliot, I appreciate it.
Although, I should mention that this problem is still occurring even when I disable all plugins (except ACF) and use a different theme. I will see if I can get a programmer to look into this for me.
Yes, both statements display the same data.
Sorry, I was totally confused, I get it now. Yes, the data is correct. What do I do now?
Yes, it’s displaying all the titles and post_ids correctly.
Out of 12 posts, 4 of them are showing a different number in the “array”. Is that something important to note? So 8 show the number “78” and 4 show the number “75”.
So when I do that, it displays the title of the first post, and then this:
Array
(
[0] => 78
)
And then it loops through the other entries but doesn’t output any data for them, just HTML.
Hmm, I don’t know (I’m not a super advanced WP programmer). But I did a search in the code for “setup_postdata” on the template and in my functions file and didn’t come up with anything. I also tried removing any custom code I’ve added and the same thing happens when I add the following to my template. It’s like it’s looping through, but not showing any data other than the first item. But if I remove the code below, it works fine.
<?php
$posts = get_field('store-type');
echo '<pre>';
print_r( $posts );
echo '</pre>';
?>
The template (taxonomy-product_type.php) is supposed to display items in a particular taxonomy. My custom post type is “products” and my taxonomy is “product_type”. So for example, it should just show me items in the “book” or “toys” category when I go to each URL.
Everything works fine when I remove the relationship data, it loops through all the right entries. But once I add in the ACF code it breaks.
I just changed the code and it does output the relationship data for the first entry. But then it shows empty HTML (h2, pre tags) for the other posts in the loop. So the same thing seems to be broken…
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.