Support

Account

Home Forums General Issues Relationship Field in WP_Query

Solving

Relationship Field in WP_Query

  • Hi,

    I would like to use a relationship field with a post object in a custom query.
    I’m using the following code to query pages:

    <?php $args = array(
    	'post_type'      => 'page',
    	'posts_per_page' => -1,
    	'post_parent'    => $post->ID,
    	'order'          => 'ASC',
    	'orderby'        => 'menu_order'
    	);
    
    $count = 2;
    $subservice = new WP_Query( $args );
    
    if ( $subservice->have_posts() ) : ?>
    <?php while ( $subservice->have_posts() ) : $subservice->the_post(); $count++?>
    
    <h1><?php the_title(); ?></h1>
    <div class="description">
        <?php the_content(); ?>
    </div>
    
    <?php 
    	$related_posts = get_field('reference_1');
    	if( $related_posts ): ?>
    <ul>
        <?php foreach( $related_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>
            <span>Custom field from $post: <?php the_field('author', $p->ID); ?></span>
        </li>
        <?php endforeach; ?>
    </ul>
    	<?php endif; ?>
    
    <?php endwhile; ?>
    <?php endif; wp_reset_query(); ?>

    Within this query I would like to get the values from the relationship field. I tried it like this:

    <?php 
    $related_posts = get_field('reference_1');
    if( $related_posts ): ?>
    <ul>
    	<?php foreach( $related_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>
    		<span>Custom field from $post: <?php the_field('author', $p->ID); ?></span>
    	</li>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>

    Strangely like this I’m not getting the title of the related post. The output I’m getting is just the title of the page in the first query. What am I doing wrong?

    Thanks, Cara

  • I think that this

    
    <?php 
    $related_posts = get_field('reference_1');
    if( $posts ): ?>
    

    should probably be

    
    <?php 
    $related_posts = get_field('reference_1');
    if( $related_posts ): ?>
    
  • Dear John, thank for the advice. This was a mistake that happened copying the code. Without the error it doesn’t work either. Thanks anyway!

  • I cannot see any other issues in the portion of code that you posted, the problem could be with some other part of the code. More than likely I’d need to see the entire loop that’s running on the query posted in the first code snippet.

  • <?php get_header(); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div id="service" class="leistungen before-hidden after-hidden-top">
    	<div class="pagepiling-container">
    		<article id="pagepiling">
    			<section class="pp-section" id="section1">
    				<div class="intro">
    					<h1><?php the_title(); ?></h1>
    					<div class="container-left">
    						<div class="wiper bottom-up">
    							<?php $images = get_field('sliderbox_links');
    							if( $images ): ?>
    							<?php foreach( $images as $key => $image ): ?>
    							<img src="<?php echo $image['url']; ?>" class="img <?php // echo $key==0 ? 'active':''; ?>" alt="<?php echo $image['alt']; ?>" />
    							<?php endforeach; ?>
    							<?php endif; ?>
    						</div>
    					</div>
    					<div class="container-right">
    						<div class="wiper top-down">
    							<?php $images = get_field('sliderbox_rechts');
    							if( $images ): ?>
    							<?php foreach( $images as $key => $image ): ?>
    							<img src="<?php echo $image['url']; ?>" class="img <?php // echo $key==0 ? 'active':''; ?>" alt="<?php echo $image['alt']; ?>" />
    							<?php endforeach; ?>
    							<?php endif;  ?>
    						</div>
    					</div>
    				</div>
    			</section>
    
    			<section class="pp-section pp-scrollable" id="section2">
    				<div class="overview">
    					<div class="content-box as-heading _fadeInBlock" style="background-image: url('<?php bloginfo('stylesheet_directory'); ?>/assets/img/section_header.png');" >
    						<div class="header">
    							<?php if( get_field('headline') ): ?>
    							<h1><?php the_field('headline'); ?></h1>
    							<?php endif; ?>
    							<?php if( get_field('subtitel') ): ?>
    							<p class="subhead">
    								<?php the_field('subtitel'); ?>
    							</p>
    							<?php endif; ?>
    						</div>
    					</div>
    
    					<div class="overview-text _fadeInBlock">
    						<?php the_content(); ?>
    					</div>
    
    					<div class="row btn-container _fadeInBlock">
    						<?php
    						$pages = get_pages( array(
    							'child_of' => $post->ID,
    							'sort_order' => 'asc',
    							'sort_column' => 'post_date')
    						);
    						foreach ( $pages as $page ) {
    							$post_slug=$page->post_name;
    							$btn = '<div class="col-lg-4 col-md-6">
    							<a href="#' . $post_slug . '" class="btn default"><span>';
    							$btn .= $page->post_title;
    							$btn .= '   </span></a>
    							</div>';
    							echo $btn; } ?>
    					</div> 
    				</div>
    			</section>
    
    			<?php
    
    			$args = array(
    				'post_type'      => 'page',
    				'posts_per_page' => -1,
    				'post_parent'    => $post->ID,
    				'order'          => 'ASC',
    				'orderby'        => 'menu_order'
    				);
    
    			$count = 2;
    			$subservice = new WP_Query( $args );
    
    			if ( $subservice->have_posts() ) : ?>
    			<?php while ( $subservice->have_posts() ) : $subservice->the_post(); $count++?>
    
    			<section class="pp-section" id="section<?php echo $count; ?>">
        <div class="fadearound">
            <div class="bgfadearound loading" style="background-image: url('<?php bloginfo('stylesheet_directory'); ?>/assets/img/bg_01.jpg');"></div>
            <div class="container-left">
                <div class="content-left">
                    <div class="content-box cb-big">
                        <div class="header">
                            <h1><?php the_title(); ?></h1>
                            <p class="subhead">
                                <?php if( get_field('subtitel') ): ?>
                                <?php the_field('subtitel'); ?>
                                <?php endif; ?>
                            </p>
                        </div>
                        <div class="description">
                            <?php the_content(); ?>
                        </div>
                    </div>
                </div>
            </div>
            <div class="container-right">
                <?php 
    			$related_posts = get_field('reference_1');
    			if( $related_posts ): ?>
                <ul>
                    <?php foreach( $related_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>
                        <span>Custom field from $post: <?php the_field('author', $p->ID); ?></span>
                    </li>
                    <?php endforeach; ?>
                </ul>
                <?php endif; ?>
            </div>
        </div>
    </section>
    			<?php endwhile; ?>
    
    			<?php endif; wp_reset_query(); ?>
    
    		</article>
    	</div>
    </div>
    <?php endwhile; endif; ?>
    <?php get_footer(); ?>
  • After looking this over, the only real problem I can see is that there is no endif; for if( $related_posts ):

  • another problem is that there are no closing tags for these

    
    <section class="pp-section" id="section<?php echo $count; ?>">
    				<div class="fadearound">
    					<div class="bg
    
  • see my notes in comments. I’m not sure if this will cause the problem your having or not.

    
    
          if ( $subservice->have_posts() ) : ?>
          <?php while ( $subservice->have_posts() ) : $subservice->the_post(); $count++?>
    
          <section class="pp-section" id="section<?php echo $count; ?>"><!-- no end tag -->
            <div class="fadearound"><!-- no end tag -->
              <div class="bgfadearound loading" style="background-image: url('<?php bloginfo('stylesheet_directory'); ?>/assets/img/bg_01.jpg');"></div>
              <div class="container-right"><!-- no end tag -->
              <?php 
                $related_posts = get_field('reference_1');
                if( $related_posts ): // no endif ?>
                <ul>
                  <?php foreach( $related_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>
                    <span>Custom field from $post: <?php the_field('author', $p->ID); ?></span>
                  </li>
                <?php endforeach; ?>
                </ul>
          <?php endwhile; ?>
    
          <?php endif; wp_reset_query(); ?>
    
  • Dear John,

    thanks for your messages!
    I’m really sorry, but this obviously all happened during copying the code. This unfortunately doesn’t solve my problem.

  • Sorry, I’m not seeing anything in your code that should be causing an issue. I could be missing it. At this point I would make a backup of this template and start strippigng things out as well as echoing values to see if I could figure out where it’s going wrong.

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

The topic ‘Relationship Field in WP_Query’ is closed to new replies.