Home Forums ACF PRO Querying relationship fields


Querying relationship fields

  • The tutorial on this page: works like a charm.

    However, i’m looking for an addition:

    Let’s say doctor 5 works in 2 locations (brisbane and sydney), how would get a list of all locations doctor 5 works at? What would that query look like?

    In other words: how do i query posts (locations) associated with posts in another post type (doctors)?

    Thanks! 🙂

  • Hi @tva

    I believe you can do it by using the normal Relationship field code. This page should give you more idea about it:

    Hope this helps 🙂

  • Hi, thanks for your reply.

    I’ll try and explain my situation, for example: i have 2 post types “areas” and “listings”. All listings can be assigned to 1 area (via the relationship field). Each listing can have 1 or more categories.

    Let’s say i have 3 areas: Amsterdam, New York and Sydney.
    Let’s say i have 3 listings: Listing 1, Listing 2 and Listing 3.
    Let’s say Listing 1 is assigned to Amsterdam, Listing 2 is assigned to New York and Listing 3 is assigned to Sydney.
    Let’s say both Listing 1 and Listing 3 have a category called “restaurant”.

    What i want to be able to do is create an archive of areas and query those archives by the category assigned to the listings.

    So normally the archive page for areas would show Amsterdam, New York and Sydney. With the query i want to built i want it to only show Amsterdam and Sydney because these areas are related to the listings that have the “restaurant” category.

    It’s a bit complex and i actually nearly there. However i’m not sure if im going about this the right way.

    On a side note my relationship field returns an array that looks like:

    Array ( [0] => Array ( [0] => 177 ) [1] => [2] => Array ( [0] => 164 ) )

    While for it to become useful it should look like:

    Array ( [0] => 177 [1] => 174 )


  • Hi @tva

    I’m afraid that is hard to achieve and will consume a lot of resources. Basically, you can get the IDs of the posts that have “restaurant” as the category. After that, you can create queries by looping through these IDs to check if the value contains one of the ID. Unfortunately, it will generate a very long query when you have a lot of posts in a category.

    What I suggest is to create a dummy field group for your Area post type to list all of the categories from the selected listing. That way, you can query the area based on the dummy field instead.

    You can do it by using the acf/save_post hook to get all of the categories of the selected listings when you save the post and then use the update_field() function to update the dummy field.

    I hope this makes sense 🙂

  • James, thanks for your reply.

    I’ve managed to get it working but i will definetly give your solution a try (and see which one runs better).

    What i did was: i created a new loop with all listings that are in the category “restaurant”. Based on the results i created an array of ID’s.

    In the main loop for the archive page i used the array i just created in the ‘post__in’ query variable and voila.

    Would you say this consumes a lot of resources?

    I’m trying to go about this as simple and smooth as possible so i’m definetly want to go with the best-practice.

  • Hi @tva

    I’m a little bit lost in your current setup, so I’m sorry if I got it wrong.

    By using your method, I believe you will get the list of listings instead of areas. I thought you want to list areas that have listings for a certain category in a relationship field:

    So normally the archive page for areas would show Amsterdam, New York and Sydney. With the query i want to built i want it to only show Amsterdam and Sydney

    Maybe I got it wrong. If you want, could you please share some screenshots of your setup (custom post types, custom taxonomies, and the field group) and the result that you expected on the front end?


  • This reply has been marked as private.
  • James, i managed to get it working!

    Thanks for all your help.

    I’ve got one additional question though, it’s relationship field related:

    Would it be possible (and if yes how) to create an admin column for the relationship field? So the relationship will be visible on my custom post type overview page?

  • Hi @tva

    I’m glad that you got it working 🙂

    Regarding your question, do you want to be able to update the relationship from the overview page (front-end) or not?

    If you want to update it, you need to use the acf_form() function. If you only want to show it, please take a look at this page:

    I hope this helps!

  • James,

    There would be no need to update anything, i’d just like to list the value of my relationship field next to the author, categories, tags etc.

    Sometimes i want to just know what relationship a post has, i’d have to edit it to see it .. would be neat if there was a admin column for it! 🙂

  • Hi Derk,

    I’m sorry I think I misunderstood your question.

    I believe you want to add an admin column for the post list on the backend. For something like that, please take a look at this page:

    Keep in mind that it’s an old article, but I guess it’s still applicable. You can also check this page to learn more about it:


  • James,

    It was actually quite easy, since the column is there purely for reference (no need to filter etc.). Perhaps the code is useful for someone else! 🙂

    function build_listing_column_head( $columns, $post_type = 'listing' ) {
    	if( ! is_admin() ) {
    	if( $post_type == get_post_type() ) {
    		$columns['relation'] = esc_html__( 'Relation', 'lystr' );
    	return $columns;
    add_action( 'manage_listing_posts_columns', 'build_listing_column_head', 10, 2 );
    function build_listing_column_content( $column, $post_id ) {
    	global $post;
    	if( ! is_admin() || 'relation' !== $column ) {
    	$relation = get_field( 'relationship', $post->ID, false );
    	if( ! empty( $relation ) ) {
    		$id = implode( ',', $relation );
    		$title = get_the_title( $id );
    		echo $title;

    Thanks for your help! 🙂

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

You must be logged in to reply to this topic.