Skip to content

WordPress: Use WP-query to retrieve Pods Relationship Custom Post Type Content

When you try to retrieve a Pods relationship custom post type content, you will find that you cannot get the  post content with the id. this is because “the_content()” do not allow to specific a post id.

Also, if you are using Siteorigin site builder, you will find out that getting the “post_content” field, your layout will not work.Β I found a practical solution, that is getting the post content with a WP_query loop, using the related post id into the query, getting Siteorigin working as expected.Β Here the full query, on this case, i’ m retrieving from the Custom Post Type “room” the “hotel” content and output it into a template.

<?php
//get Pods object for current post
$pod = pods( 'room', get_the_id() );
//get the value for the relationship field
$related = $pod->field( 'hotel' );
//loop through related field, creating links to their own pages
//only if there is anything to loop through
if ( ! empty( $related ) ) {
foreach ( $related as $rel ) { ?>

<?php
//get id for related post and put in ID
//for advanced content types use $id = $rel[ 'id' ];
$id = $rel[ 'ID' ];
$content = get_post_field('post_content', $id);
?>

<?php

$args = array(
'p' => $id,
'post_type' => 'any');

// La Query
$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
the_content();
endwhile;
wp_reset_query();
wp_reset_postdata();

?>

<?php } //end of foreach
} //endif ! empty ( $related )
?>