Awesome ACF

Query Posts by ACF Relationship Field

Reverse-query posts that reference a specific post in an ACF Relationship or Post Object field.

Last updated

Reverse-query posts that reference a specific post in an ACF Relationship or Post Object field.

ACF stores relationship field values as serialised arrays. Use LIKE to search within them:

<?php
// Find all posts that reference post ID 42 in their 'related_articles' field.
$related = new WP_Query( [
    'post_type'  => 'post',
    'meta_query' => [
        [
            'key'     => 'related_articles',
            'value'   => '"42"',
            'compare' => 'LIKE',
        ],
    ],
] );

The double quotes around the ID ('"42"') are important. They match the serialised array format and prevent false matches (e.g. 142 or 421).

For a Post Object field (single value, not serialised):

<?php
// Find all books assigned to author post ID 15.
$books = new WP_Query( [
    'post_type'  => 'book',
    'meta_query' => [
        [
            'key'     => 'author',
            'value'   => 15,
            'compare' => '=',
        ],
    ],
] );

Bidirectional lookup. Display related posts on the current post’s page:

<?php
$current_id = get_the_ID();

// Forward: posts this post references.
$selected = get_field( 'related_articles' );

// Reverse: posts that reference this post.
$referencing = new WP_Query( [
    'post_type'  => 'post',
    'meta_query' => [
        [
            'key'     => 'related_articles',
            'value'   => '"' . $current_id . '"',
            'compare' => 'LIKE',
        ],
    ],
    'post__not_in' => [ $current_id ],
] );

Note: ACF 6.2+ supports bidirectional relationships natively via the field settings, which automatically keeps both sides in sync.

Stay updated

Get ACF tips and new extensions in your inbox. No spam. Unsubscribe anytime.