Skip to content

Instantly share code, notes, and snippets.

@daltonrooney
Forked from phillipadsmith/gist:2695431
Created July 26, 2012 14:55
Show Gist options
  • Save daltonrooney/3182528 to your computer and use it in GitHub Desktop.
Save daltonrooney/3182528 to your computer and use it in GitHub Desktop.
Example of usign Advanced Custom Fields and querying the reverse relationship. Discussion: http://www.advancedcustomfields.com/support/discussion/1979/how-to-getting-the-reverse-relationship-from-a-relationship-field
<?php $args = array(
'numberposts' => -1,
'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'custom_post_type_here',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'custom_field_key_name',
'value' => $post->ID,
'compare' => 'LIKE'
)
)
);
$posts_array = get_posts( $args );
if( $posts_array ) {
echo '<ul>';
foreach( $posts_array as $related ) {
echo '<li>';
echo '<a href="' . get_permalink( $related->ID ) . '">' . $related->post_title . '</a>';
echo '</li>';
}
echo '</ul>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment