Support

Account

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

Unread

Reverse querying an ACF relationship field inside a function

    • voneff

    • October 14, 2020 at 8:54 pm

    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?

Viewing 1 post (of 1 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.