I’ve tried to use reset_postdata
before, and it didn’t help. Maybe I’m using it the wrong way? It’s not doing anything for the better, but it’s also not messing anything up. It’s really confusing.
Where would you place the reset_postdata
function in the code I posted above? I have already tried placing it at the end of the loops 🙁 I’m lost.
I did some debugging and it actually seems like the post ID is changing between the loops for some reason. Got any idea why it could do that when I’m not using setup_postdata
?
Hey. Thanks for your answer. There is definitely more to the story, but I thought it would be a lot to post and possibly irrelevant. I’m not using setup_postdata. Here is the full code for loop-events.php
(I’m using if_single so that I can just load the same loops using get_template_part, but the same problem occurs if I copy the whole loop into the single page)
<div id="event-archive" class="clearfix">
<?php if (is_single()) { $uargs = array( 'post_type' => 'events',
'posts_per_page' => 3,
'meta_key' => 'eventdate', // name of custom field
'orderby' => 'eventdate',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'attending', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
); ?>
<?php
} else {
$uargs = array (
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'eventdate', // name of custom field
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
}
$uloop = new WP_Query( $uargs ); ?>
<?php while ( $uloop->have_posts() ) : $uloop->the_post();
?>
<?php
$date = get_field('eventdate');
// $date = 19881123 (23/11/1988)
// extract Y,M,D
$y = substr($date, 0, 4);
$m = substr($date, 4, 2);
$d = substr($date, 6, 2);
// create UNIX
$time = strtotime("{$d}-{$m}-{$y}");
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div id="event-1" class="event">
<div id="event_datestamp"><p class="hdm"><?php echo date('M', $time); ?></p><p class="hdd"><?php echo date('d', $time); ?></p></div>
<?php $eurl = get_field('eventurl'); ?>
<h2 class="event-title"><a href="<?php echo get_permalink($eurl->ID); ?>" title="<?php printf( esc_attr__( 'Read about this event', 'wpzoom' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<span class="att-artists">
<?php
$posts = get_field('attending');
$i = 0;
if( $posts ): ?>
<?php
foreach( $posts as $p ): ?>
<?php
$i++;
?>
<?php
if( $i == 1 )
{
echo '';
}
elseif( $i == count($posts) )
{
echo ', ';
}
elseif( $i > 1 )
{
echo ', ';
}
?>
<a href="<?php echo get_permalink( $p->ID ); ?>" title="See <?php echo get_the_title( $p->ID ). '’s profile'; ?>"><?php echo get_the_title( $p->ID ); ?></a>
<?php endforeach; ?>
<?php endif; ?></span></div>
<div id="event-2" class="event"><div class="eventmeta"><span class="event-meta1"><?php the_field('eventloc'); ?></span>
<span class="event-meta2"><?php echo date('d/m/Y', $time) . ' ('; the_field('age'); echo ')'; ?></span>
</div></div>
<div id="event-3" class="event"><div id="gettick"><span class="event-tkt"><a href="<?php the_field('biturl'); ?>" target="_blank">Book Now</a></span></div></div>
</div>
<?php
endwhile;
?>
</div>
All my loops work fine individually on the single-artist page, and on their individual archive pages. But this, for some reason, can only work once:
'meta_query' => array(
array(
'key' => 'attending', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
The fields are correct and I’ve checked so that there are no mistakes with variables etc.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.