Skip to content

Instantly share code, notes, and snippets.

@cameronbaney
Last active December 11, 2015 07:48
Show Gist options
  • Save cameronbaney/4568453 to your computer and use it in GitHub Desktop.
Save cameronbaney/4568453 to your computer and use it in GitHub Desktop.
This is for getting relationships in WordPress
<?php
// Variable for post ID
$pID = '"' . $post->ID . '"';
// Query the relationship
$relpa = new WP_Query(array('post_type'=>'page','posts_per_page'=>'-1','post_parent'=>'6',
'meta_query'=>array(
array(
'key' => 'rel_attorney', // This is the custom field's name
'value' => "$pID", // This is the post ID of the current page you're on
'compare' => 'LIKE'
)
))
);
// Check if there are any relationships
if($relpa->found_posts){ ?>
<h2 class="rel-title">Practice Areas</h2>
<ul class="rel-list">
<?php }
// The loop
while ( $relpa->have_posts() ) : $relpa->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile;
// Check if there are any relationships (this is done again to close out the ul)
if($relpa->found_posts){ ?>
</ul>
<?php
} // Close the check if there are any relationships if statement
wp_reset_postdata(); // Reset the post
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment