Home Forums General Issues Reverse querying an ACF relationship field inside a function


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:

    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:

    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
    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
    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(
    									'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?

  • have you a solution? i have the same issue

  • Hi @elchesca

    I ended up going the plugin route.

    If I remember correctly, this plugin here solved my problem easily:


Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.