Skip to content

Instantly share code, notes, and snippets.

@mrpaulphan
Last active March 10, 2017 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrpaulphan/fd519887e974c9fb000d59113b08cdfc to your computer and use it in GitHub Desktop.
Save mrpaulphan/fd519887e974c9fb000d59113b08cdfc to your computer and use it in GitHub Desktop.
Reverse Query Relationship for Advanced Custom Fields
<?php
$posts = get_posts(array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_type' => 'testimonial',
'meta_query' => array(
array(
'key' => 'related_project', // name of custom field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
if ($posts): ?>
<section class="testimonial">
<div class="page testimonial__content">
<?php foreach ($posts as $post):
setup_postdata($post);
?>
<blockquote class="testimonial__blockquote">
<div class="">
<p class="testimonial__quote"><?php the_field('testimonial') ?></p>
<span class="testimonial__author">-<?php the_title(); ?></span>
</div>
<span class="testimonial__image"><img src="http://placehold.it/259x169" alt=""></span>
</blockquote>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
</section>
<!-- ./testimonial -->
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment