Support

Account

Home Forums General Issues Reverse relationship – the_content

Solving

Reverse relationship – the_content

  • Hi there, I have a reverse relationship setup with a return format of Post Objects. I have followed your instructions (thank you) and most data comes through perfectly but can’t get the_content or the_exceprt from the relationship. I have mucked about trying get_the_content but cannot get anything to appear 🙁 Am I missing something? Renee

    My code:

    <?php 
        $items = get_posts(array(
    	 'post_type' => 'post',
    	 'meta_key' => 'nursery',
    	 'orderby' => 'date',
    	 'order' => 'ASC',
    	 'meta_query' => array(
    	     array(
    		 'key' => 'nursery', 
    		 'value' => '"' . get_the_ID() . '"', 
    		 'compare' => 'LIKE'
                     ))
    ));
    ?>  
     <?php if( $items ): ?>
     <?php foreach( $items as $item ): ?>
         <div class="news">
    	<?php 
            $photo = wp_get_attachment_url( get_post_thumbnail_id($item->ID), 'news' );
    	$content = the_content( $item->ID ); // have tried get_the_content
    ?>
     <a href="<?php echo get_permalink($item->ID); ?>"   
       <div class="nursery-news clearfix">
       <img src="<?php echo $photo  ?>" alt="<?php echo get_the_title( $item->ID ); ?>" width="180"  /> // gets thumbnail okay
      <h3 style="clear:none"><?php echo get_the_title( $item->ID ); ?></h3>
      <?php echo get_the_date( $item->ID ); ?> // gets date okay
      <?php $author = get_the_author( $item->ID ); ?> <?php echo $author; ?> // gets author okay
      <p><?php echo $content; ?></p> // no content appearing - have tried excerpt also
     </div></a>
     <?php endforeach; ?>
     <?php endif; ?>  
  • Hi Renee i have the same problem, did you get this solved somehow?

  • Hi Kylu, am yet to solve this problem.

  • Hi renee, kylu –

    the reason this won’t work is that the_content and get_the_content are only meant to be used within ‘The Loop’ and therefore do not accept a post id parameter.

    my personal solution to this would be using WP_Query directly (rather than get_posts) to create a secondary loop, but within the context of your code above you can probably get around this through the use of setup_postdata as in the example here:

    http://codex.wordpress.org/Template_Tags/get_posts#Access_all_post_data

    and remember to call wp_reset_postdata after the fact.

    EDIT: as an afterthought… you should be able to access all post data directly from your $items array the same way you are accessing the id. the only problem is that this bypasses all content filters so you would have to apply them manually.

    something like:

    $content = apply_filters( 'the_content', $item->post_content );

  • Hi RBmac,

    Been away from the desk. Thanks for responding. Of course, not in the loop – makes total sense and now have it working. Setup_postdata was the answer.

    Code for anyone looking for it:

    <?php
    $args = array(
    	'post_type' => 'post',
    	'posts_per_page' => 1,
    	'meta_query' => array(
    		array(
    			'key' => 'field', // name of custom field
    			'value' => '"' . get_the_ID() . '"',
    			'compare' => 'LIKE'
    		)
    	)
    );
    $lastposts = get_posts( $args );
    foreach ( $lastposts as $post ) :
      setup_postdata( $post ); ?>
    	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    	<p><?php the_content(); ?>
    <?php endforeach; 
    wp_reset_postdata(); ?>
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.