Meta Query - Retrieving values from an ACF Post Object or Relationship field

It doesn’t seem possible to create a Meta Query in the Query Loop when comparing a relationship or post object field against the post title of the active item?
In the screenshot I have created 2 fields and 2 Meta Queries. The Query relating to the Service Text field works as expected, however the query relating to the Service Post Object field doesn’;t return a value.

Not raised as a bug as it may be an ACF issue but can’t find a workaround

Ran into this a few days ago and did it like this:

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    // Use an ACF custom field to restrict the query to a set of posts
    if ( $element_id == 'knmaqa' && function_exists('get_field') ) {
        $query_vars['post__not_in'] = get_field('relevante_rechtsgebiete');
    }

    return $query_vars;
}, 10, 3 );

knmaqa is the ID of the loop and relevante_rechtsgebiete is the acf relationship field

In my case, I needed to get all the posts that aren’t in the relationship field. If you wanna loop and show all posts from a relationship field though, you can just select it straight in the query builder

1 Like

Hi Martin - great tip I’ll give this a go. I had found a messier function that basically copies the postid content of the ACF object field into an ACF text field. It works but not the cleanest solution

Just re-read your question and I think you could try:

'post__not_in' => [get_queried_object_id()]

To exclude the post you’re on while showing all others from the relationship field. Haven’t tested it, but in theory it should work haha.

Or if u wanna apply that directly to the relationahip field, no matter where you loop over it, this seems like a nifty lil snippet

I don’t know if you’ve already found a reasonable fix for your problem, given this thread is pretty dated. But I just ran into the same problem with filtering by an ACF Relationship/Post Object.

I found that by checking against {post_id} instead of {post_title} in the Meta Query gave me a successfull output :+1:t2:

Hi, can you please exemplify with a couple screenshots of the queries you made ? Pulling hairs trying to solve the same thing

Hi

you can actually use

"{post_id}"

and the acf relationship field in the meta query to run a bidi-relationship query.

But you must put the {post_id} within quotation marks.

See here:

The full explanation is here:

Cheers

Patric