Home › Forums › General Issues › Reverse querying an ACF relationship field inside a function
Hi everyone,
I got stuck with the following issue:
I have two custom post types called “Biography” and “Photo” on my site.
“Photo” has an ACF relationship field where I can connect it to one “Biography”. I want to display these relationships on the front-end built with Elementor. I was able to build a custom query for displaying the related “Biography” on the “Photo” single post through the Elementor Posts widget, based on this:
https://github.com/elementor/elementor/issues/4916#issuecomment-422161001
I was not able though to build a query that reverses the relationship and shows all “Photo” posts on the related “Biography” single post. It should be possible though per the ACF documentation:
https://www.advancedcustomfields.com/resources/querying-relationship-fields/
Here’s my code:
/*-----------------------------------------------------------------------------------*/
/* Elementor x ACF: Add a custom query to use the ACF relationshop as a source of posts in the Elementor Posts widget
/* @link https://github.com/elementor/elementor/issues/4916
/*-----------------------------------------------------------------------------------*/
add_action( 'elementor_pro/posts/query/acpa_photo_get_related_bio', function ( $query ) { $postid = get_the_ID(); $ids = get_post_meta($postid, 'acpa_photo_related_biography', true); if ( $ids ) { $query->set( 'post__in', $ids ); } } );
/*-----------------------------------------------------------------------------------*/
/* Elementor x ACF: Reverse-querying a relationship between Biographies and Photos to display a list of Photo posts on Biography post
/* @link https://www.advancedcustomfields.com/resources/querying-relationship-fields/
/*-----------------------------------------------------------------------------------*/
add_action( 'elementor_pro/posts/query/acpa_bio_get_related_photos', function ( $query )
{ $postid = get_the_ID(); $ids = get_post_meta(array(
'post_type' => 'photo',
'meta_query' => array(
array(
'key' => 'acpa_photo_related_biography', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
}
);
The first action “acpa_photo_get_related_bio” is working, the second one “acpa_bio_get_related_photos” is not. I guess it’s *just* a matter of sticking that query inside a function – but as you probably have gathered by now, my PHP knowledge is rather mediocre. 😉
Would anyone be willing to give me a little push in the right direction?
Hi @elchesca
I ended up going the plugin route.
If I remember correctly, this plugin here solved my problem easily:
https://wordpress.org/plugins/post-2-post-for-acf/
Cheers
Alex
Not doing reverse relationship queries was the reason that plugin was created.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.