Support

Account

Home Forums Front-end Issues Problem Using Relationship Field & CPT Query

Solved

Problem Using Relationship Field & CPT Query

  • I’m trying to use the Relationship field to no success. Here’s the scenario.

    I have two custom post types – Issues and Articles. In the Articles post type, there is a custom field named “Issue” which is a relationship, which allows someone to choose the issue, from the Issues CPT, that the article is in.

    On the single-issue.php file, I’m then trying to query the articles that are in that issue based on the value chosen in the relationship field for a particular article.

    I’ve tried following all the tutorials and looking through the support threads but have had no luck so far.

    Here’s the code I’m using:

    <?php 
    
    // args
    $args = array(
    	'post_type'	=> 'article',
    	'meta_query' => array(
    		array(
    			'key' => 'issue',
    			'value' => '"' . get_the_ID() . '"',
    			'compare' => 'LIKE'
    		)
    	)
    );
     
    // get results
    $the_query = new WP_Query( $args );
     
    // The Loop
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<ul>
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
     
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

    Please help!

  • Is it a relationship field or a post object field? The reason I ask is that I don’t see anything wrong with your code.

  • It’s a relationship field, set to return format of post IDs.

  • Can you post the entire code of single-issue.php

  • `<?php get_header(); ?>

    <?php include( TEMPLATEPATH . ‘/includes/red-bar.php’ ); ?>

    <div class=”container-fluid content”>
    <div class=”row-fluid”>
    <p class=”breadcrumbs”><span xmlns:v=”http://rdf.data-vocabulary.org/#”><span typeof=”v:Breadcrumb”><a href=”<?php bloginfo(‘siteurl’); ?>” rel=”v:url” property=”v:title”>Home</a> » <span><a href=”<?php bloginfo(‘siteurl’); ?>/get-informed/”>Get Informed</a></span> » <span><a href=”<?php bloginfo(‘siteurl’); ?>/get-informed/dispatch-from-jerusalem/”>Dispatch from Jerusalem</a></span> » <span class=”breadcrumb_last”><?php the_title(); ?></span></span></span></p>
    <?php do_action(‘addthis_widget’,get_permalink($post->ID), get_the_title($post->ID), ‘above’); ?>

    </div>
    <div class=”row-fluid dispatch-issue”>
    <div class=”span3″>
    <?php the_post_thumbnail(); ?>

    <p class=”e-dispatch”><a href=”<?php echo the_field(‘external_url’); ?>” target=”_blank”>View e-Dispatch</a></p>
    </div>
    <div class=”span9″>
    <h1><?php the_title(); ?></h1>

    <?php

    // args
    $args = array(
    ‘post_type’ => ‘article’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘issue’,
    ‘value’ => ‘”‘ . get_the_ID() . ‘”‘,
    ‘compare’ => ‘=’
    )
    )
    );

    // get results
    $the_query = new WP_Query( $args );

    // The Loop
    ?>
    <?php if( $the_query->have_posts() ): ?>
    <ul>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <li>
    <a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>
    </li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>

    <?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>

    </div>
    </div>

    <?php include( TEMPLATEPATH . ‘/includes/explore-subjects.php’ ); ?>

    </div>

    <?php get_footer(); ?>

  • The problem is that you don’t have a main WP loop so get_the_ID() is not returning anything.

    I’m only guessing what where this needs to go

    
    <?php 
        while(have_posts()) : the_post();
          ?>
    <div class="row-fluid dispatch-issue">
    <div class="span3″>
    <?php the_post_thumbnail(); ?>
    
    <p class="e-dispatch"><a href="<?php echo the_field('external_url'); ?>" target="_blank">View e-Dispatch</a></p>
    </div>
    <div class="span9″>
    <h1><?php the_title(); ?></h1>
    
    <?php
    
    // args
    $args = array(
    'post_type' => 'article',
    'meta_query' => array(
    array(
    'key' => 'issue',
    'value' => '"' . get_the_ID() . '"',
    'compare' => '='
    )
    )
    );
    
    // get results
    $the_query = new WP_Query( $args );
    
    // The Loop
    ?>
    <?php if( $the_query->have_posts() ): ?>
    <ul>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>
    
    <?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
    
    </div>
    </div>
          <?php 
        endwhile;
    ?>
    
  • I tried that and it still didn’t work. Specifically, I added…

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    ...
    
    <?php endwhile; endif; ?>

    …around the whole thing.

  • With the addition of the standard WP loop to the template I really don’t see anything else wrong. The only think I can think of is that get_the_ID() is not working

    Try echoing the value returned from that and see what you get

    
    <h1><?php the_title(); ?></h1>
    <?php echo 'The Post ID is = ('.get_the_ID().')';
    
  • That’s working – the ID is successfully being output.

  • Update: I figured out that it’s a problem with the content and not with the code. All of the articles that have the issue custom field – the data was all imported from another website. Something must have happened during the import process in the database where even though the article had a particular issue associated with it through that relationship field, the value still wasn’t being recognized.

    Once I start going through articles and updating the fields, the code worked properly. So it’s not a problem on the code end, it’s with the data. At least the code we’re using, which is what we determined above, is working fine.

    Thanks for the help!

  • So, the problem was that either the acf field key field was not imported or the field key for the fields on the two sites were different. Each field that ACF saves has a corresponding value in the DB that holds the field key. If that is not in the database, or the key is incorrect then ACF does not know what to do with the value. You’ll either get the wrong output or nothing at all unless it a simple text field.

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

The topic ‘Problem Using Relationship Field & CPT Query’ is closed to new replies.