Support

Account

Home Forums Front-end Issues Multiple loops comparing meta value problem

Solving

Multiple loops comparing meta value problem

  • Hello.

    So I have two different post types, events and release. Both has a relationship field where you can pick artists from my third post type, artist.

    In the single post for a specific artist, I’m trying to loop the events and releases from the artist.

    In single-artist.php, I’m loading the loop templates:

    <?php get_template_part( 'loop', 'events' ); ?>
    <?php get_template_part( 'loop', 'release' ); ?>

    Below is the query that matches the artist ID.

    <?php 
    
    if (is_single()) {
    
    $args = array( 'post_type' => 'release', 'posts_per_page' => -1, 
    
    	'meta_query' => array(
    								array(
    									'key' => 'artist-name', // name of custom field
    									'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    									'compare' => 'LIKE'
    								)
    							)
    				); ?>
    
                    <?php } else { 
    
    				$args = array( 'post_type' => 'release', 'posts_per_page' => -1);
    
    				}?>

    It works just fine, but only ONCE! The query is pretty much the same on events and releases, but only the “get_template_part loop” I place above the other works. So if the events is on top, I see the artists events. If the releases are on top, I see the artists releases. But never both.

    I have done some debugging and it definitely seems like the problem is in ‘meta_query’.

    I hope this explaination is enough, and hopefully someone can give me an answer to why the query only works the first time.

  • Hi @PierreDST

    Is the above the only code which you use? Or is there more to the story?
    Perhaps you are using a setup_postdata function in your loop?

    This will modify the global $post object and effect which post you load the ACF data from.

    Perhaps you can do some debugging through your code to check that the post_id does not change.

    Thanks
    E

  • 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.

  • 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?

  • Hi @PierreDST

    I believe that $uloop->the_post(); is the equivalent of setup_postdata.

    This will override the global post. Int he WP_Query docs, it talks about a function called reset_postdata. I highly suggest to read over that section.

    Thanks
    E

  • 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.

  • Hi @PierreDST

    You use this function after the while( have_posts() ) loop.

    Thanks
    E

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

The topic ‘Multiple loops comparing meta value problem’ is closed to new replies.