Support

Account

Home Forums ACF PRO Relationship Problem on Archive page

Solved

Relationship Problem on Archive page

  • Hi there, I noticed that on a custom post type archive page i could not use relationship field (either with setup_postdata or without it) cause it crashes the query. I had to use the WP_Query methof to make it works. That’s a bit strange 🙁

    WP 4.4.2, last ACF PRO

  • Hi @gleenk

    Can you post the code you attempt to use which crashes the query?

  • <?php $posts=get_field( 'ricette_correlate'); if( $posts ): ?>
    <div class="related-ricette">
        <ul>
            <?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
            <li>
                <a href="<?php echo get_permalink( $p->ID ); ?>">
                    <?php echo get_the_title( $p->ID ); ?></a>
            </li>
            <?php endforeach; ?> </ul>
    </div>
    <!--/.related-ricette-->
    <?php endif; ?>
    

    this is inside homepage (created with a custom page tpl, inside the post-type query).

    Complete code of custom query:

    <?php $args = array(
    	'post_type' => 'corsi',
    	'posts_per_page' => 6
    );
    $the_query = new WP_Query( $args);
    // The Loop
    if ( $the_query->have_posts() ) : ?>
    
    <div class="listing-corsi">
    	<div class="col-group-row">
    
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<div class="col one-third m-1-2 s-1-1">
    			<div class="item-corso" data-mh="grp-item-corso">
    				<figure class="bwWrapper main-thumb"><?php the_post_thumbnail('t630x364');?></figure>
    				<div class="entry-content">
    					<h2 class="entry-title"><?php the_title();?></h2>
    					<div class="meta">
    						<div class="date"><?php the_field('data_corso');?> / <?php the_field('orario_corso');?></div>
    					</div><!--/.meta-->
    					<div class="entry-text">
    						<?php the_content();?>
    					</div><!--/.entry-text-->
    					<div class="show-related-ricette">
    						<span class="icon-top-arrow"></span> Ricette correlate al corso
    					</div><!--/.show-related-ricette-->
    					
    					<?php 
    					$posts = get_field('ricette_correlate');
    					if( $posts ): ?>
    					<div class="related-ricette">
    						<div class="close"><span class="icon-close"></span></div>
    					    <ul>
    						<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
    					        <li>
    					            <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
    					        </li>
    					  	<?php endforeach; ?>
    					    </ul>
    					 </div><!--/.related-ricette-->
    					<?php endif; ?>
    					
    					<div class="cat-list">
    						<?php
    						global $post;
    						$terms = wp_get_post_terms( $post->ID , 'tipologia_di_cucina' );
    						if ( $terms ){
    						    echo '<ul>';
    						    foreach ( $terms as $term ) {
    						      echo '<li><a href="'.get_term_link( $term ).'">' . $term->name . '</a></li>';
    						        
    						    }
    						    echo '</ul>';
    						}
    						?>
    					</div><!--/.cat-list-->
    				</div><!--/.entry-content-->
    			</div><!--/.item-corso-->
    		</div><!--/.col-->
    		<?php endwhile; ?>
    
    	</div><!--/.col-group-row-->
    </div><!--/.listing-corsi-->
    <?php endif; wp_reset_postdata(); ?>
  • Hi,

    Okay.. So first off. I don’t think this can be called an issue of ACF as it’s more about what WordPress consider is the current global post or query. What you want to do is basically nested custom queries.

    Could you give this code a try? If you notice I do use $post for the foreach loop along with setup_postdata but instead of calling wp_reset_postdata() I call a function on the custom query itself to restore that in particular.

    
    <?php $args = array(
    	'post_type' => 'corsi',
    	'posts_per_page' => 6
    );
    $the_query = new WP_Query( $args);
    // The Loop
    if ( $the_query->have_posts() ) : ?>
    <div class="listing-corsi">
    	<div class="col-group-row">
    
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<div class="col one-third m-1-2 s-1-1">
    			<div class="item-corso" data-mh="grp-item-corso">
    				<figure class="bwWrapper main-thumb"><?php the_post_thumbnail('t630x364');?></figure>
    				<div class="entry-content">
    					<h2 class="entry-title"><?php the_title();?></h2>
    					<div class="meta">
    						<div class="date"><?php the_field('data_corso');?> / <?php the_field('orario_corso');?></div>
    					</div><!--/.meta-->
    					<div class="entry-text">
    						<?php the_content();?>
    					</div><!--/.entry-text-->
    					<div class="show-related-ricette">
    						<span class="icon-top-arrow"></span> Ricette correlate al corso
    					</div><!--/.show-related-ricette-->
    					
    					<?php 
    					$posts = get_field('ricette_correlate');
    					if( $posts ): ?>
    					<div class="related-ricette">
    						<div class="close"><span class="icon-close"></span></div>
    					    <ul>
    						<?php foreach( $posts as $post ): setup_postdata($post); ?>
    					        <li>
    					            <a href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a>
    					        </li>
    					  	<?php endforeach; $the_query->reset_postdata(); ?>
    					    </ul>
    					 </div><!--/.related-ricette-->
    					<?php endif; ?>
    					
    					<div class="cat-list">
    						<?php
    						global $post;
    						$terms = wp_get_post_terms( $post->ID , 'tipologia_di_cucina' );
    						if ( $terms ){
    						    echo '<ul>';
    						    foreach ( $terms as $term ) {
    						      echo '<li><a href="'.get_term_link( $term ).'">' . $term->name . '</a></li>';
    						        
    						    }
    						    echo '</ul>';
    						}
    						?>
    					</div><!--/.cat-list-->
    				</div><!--/.entry-content-->
    			</div><!--/.item-corso-->
    		</div><!--/.col-->
    		<?php endwhile; ?>
    
    	</div><!--/.col-group-row-->
    </div><!--/.listing-corsi-->
    <?php endif; wp_reset_postdata(); ?>
    
  • Great it works, thank you :)!
    Could you specify this point better in documentation? 🙂

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

The topic ‘Relationship Problem on Archive page’ is closed to new replies.