Home Forums Front-end Issues Reverse Relationship Query Reply To: Reverse Relationship Query

  • Thanks John. That’s kinda what I thought. I looked into doing it directly via the database but since the relationship field is serialized data it’s a bit tricky (though I’m sure not impossible).

    For anyone stumbling upon this, I was able to solve it by looping through and building an array of the info I need.

    I’m using “posts” and “authors” in place of “doctors” and “locations” but the idea is the same.

    $posts = get_posts(array(
    	'post_type' => 'post',
    	'posts_per_page' => -1,
    	'meta_key' => 'author',
    	'meta_value' => ' ',
    	'meta_compare' => '!='
    $authors_list = array();
    foreach ($posts as $post) {
    	$related_authors = get_field('author', $post->ID);
    	foreach ($related_authors as $author_id) {
    		if (!in_array($author_id, $authors_list)) {
    			$authors_list[] = $author_id;
    function author_sort($a, $b) {
    	return strcmp($a->post_title, $b->post_title);
    usort($authors_list, "author_sort");
    foreach ($authors_list as $author) {
    	echo '<a href="' . get_the_permalink($author_id) . '">' . get_the_title($author->ID) . '</a><br />';