Tipp: How to Meta Query for ACF Bi-Directional Query

Hi there

just solved an issue with a custom query for an ACF bi-directional query.

If you want to find all posts for an ACF relationship field (in my example the field chochete_rezepte), but the bi-directional way, you can enter this into the meta query:

ACFBidi

What I’ve just learnt is that you have to add the " before and after the post_id. This prevents a match for “1234” when the post_id is “123”.

So make sure that the post_id is entered like this including the ":

"{post_id}"

Cheers

Patric

6 Likes

This is fantastic. I wondered if such a workaround was possible, but I tried other settings for “compare” and gave up. Thanks a lot!

1 Like

@Patric Hi there! Tried your solution but it doesn’t seem to work for me. Also tried some of the solutions described in this topic with no luck unfortunately.

I have a post object field (tried both return formats) with location being an options page, where i select posts. The ‘bricks/posts/query_vars’ solution doesn’t work for me either. I think i’ ve run out of ideas and query combinations at this point, lol. Do you have any idea why that doesn’t work for me? Thank you!

Hi

my post / solution was specifically for ACF relationship fields.

I am not using Post Object fields. I am not sure that these fields create a similar “post connection” like the ACF relationship fields do.

Anyway, I see that in your query you did not select a custom post type.

How is your setup exactly? Did you create a custom post type (CPT)? for which CPT did you create the ACF fields?

Cheers

Patric

Yes, i figured. But because the post object field was also mentioned in the other topic i gave it a try.

The ACF field is for selecting regular posts. Like this:

2

And it is located in an options page. So no CPT here! All i want to achieve is group the selected posts together. But i see that this option:

6

is just for displaying the selected posts. I am trying to figure out a meta query from this menu

7

because in some instances i would want to also add a taxonomy query or exclude them from a query etc.

Basically what i want to achieve is this: Group some posts together and then exclude them or include them in different queries, based on each case. I went the “post object” way to group them together because the process seems fairly easier to manage (search, drag and drop) than a custom field in each post. I hope i made myself clear, Patric. Cheers!

This doesn’t work for my case because the posts that are grouped together will change constantly. So the client would have to exclude them manually from the other queries each time they change.

Picture it like this: You have a blog website with featured posts in the top section of the homepage (this is the group i am trying to create via the post object field) and below that there are post categories, which should not include the featured posts (othewise they would appear twice on the page). How would you go about excluding these featured posts from the category sections below? I am trying to target them with a meta query but i can’t figure it out. The featured posts in the top section should also have custom ordering.

I could add a custom field or a taxonomy “featured” to the posts that i want to group together and maybe another numeric custom field for the ordering but that solution seems less managable when the featured posts and their order will change frequently. That’s why i was playing around with the post object field, because it has ajax search for the posts that i want to include and drag and drop for the ordering. But i couldn’t manage to exclude them from other queries (eg. the category sections).

Anyway, thanks for your time Patric.

Ok, got it.

For such a case I would use the query editor as this will give you full flexibility to code the query arguments.

In the query editor, you can first get the acf field array that contains the posts IDs that you want to include and then feed this array into the query argument like ‘post__in’.

Cheers

Patric

I’m afraid more complex queries like that are above my php knowledge level but i will give it a shot and look into the ACF documentation/forums some more. Or i will figure out a simpler solution.

Have a good one.

If I have time tomorrow, I might be able to quickly put the code together for you. Not difficult,

I just first have to create the acf fields etc


Cheers

Patric

1 Like

OK, here it goes, quick & dirty, based on your ACF Post Object field ‘selected_posts’.

Put this code into the query editor in a Bricks query on the page that has this ACF field (change the name of the ACF field and / or the post type as necessary in your case)

(Updated Code)

$theid = array_column(get_field('selected_posts'), 'ID');

return [
 'post_type' => 'post',
 'post__in' => $theid,
 'posts_per_page' => -1,
 'orderby'=> 'post__in',
];

Test if it works:

I add the ACF Post Object field into a page and add a few posts:

Posts1

I then add a query in the page in Bricks and enter the query code from above via the query editor:

Posts3

And this is the output in the frontend:

Posts2

It works.

To exclude the posts, change in the query code the ‘post__in’ into ’ post__not_in’. So just add a second query with this and then the posts will be excluded.

Cheers

Patric

By the way


If you have your ACF Post Object Field on a special page and want to create the Bricks page with the query on another page, then simply add the page ID of the page that has the ACF field (in my example the page with the ID 49037) into the first line in the code:

$value_array = get_field(‘selected_posts’, 49037);

Cheers

Patric

Hello!

When i added the field in an options page and copied the option page id into the first line of the code it didn’t work. But having the post object field in the same page as the loop works. That’s cool, i’m fine with that.

Main issue is that only the first post that i selected in the post object field gets displayed. Wondering why, because it seemed to work fine for you.

And if i have no posts in the ACF object field it still displays 1 post (the post with the highest ID in my case since i kept the ordering as is for now).

Yes, i attempted to make a completely new query and just added the code in the editor. Same result.

Note: If i setup any other query multiple posts are being displayed. So my page elements are ok.

Regarding the issue when you have the ACF field in an ACF option page and nothing is found, try adding the ‘option’ parameter into the get_field code, like this

$value_array = get_field('selected_posts', 'option');

Cheers

Patric

In case somebody needs this, here is the final code:

$theid = array_column(get_field('selected_posts'), 'ID');

return [
 'post_type' => 'post',
 'post__in' => $theid,
 'posts_per_page' => -1,
 'orderby'=> 'post__in',
];

If the ACF Post Object Field is on an ACF Options Page, then the first line should be

$theid = array_column(get_field('selected_posts', 'option'), 'ID');

Cheers

Patric

1 Like

I have been trying to get this working for our site for a solid day now, but I have NO idea what we are doing wrong.

If you have an idea what is going wrong, please let me know, I am stumped.



Hi

can you post a screen shot from the element structure in Bricks, like this one please?

Rel2

Have you selected in Bricks the query type AFC Relationship , like this one?

Cheers

Patric

OK, try this approach:

  1. Make a normal query, but select the Query Editor.

  2. In the Query Editor, type this code and replace the post_type ‘chochete’ in line 2 with your desired post type and replace the key ‘chochete_rezepte’ with your ACF Relationship Field:

return [
 'post_type' => 'chochete', 
 'posts_per_page' => -1,
 'meta_query'        => array(
     array(
            'key'       => 'chochete_rezepte',
            'value'     => '"' . get_the_ID() . '"',
            'compare'   => 'LIKE'
        ),
       ),
  'order' => 'ASC',
  'orderby'=> 'date', 
];

Make sure, that ‘Posts’ is selected as the Query Type:

In the frontend, Bricks should now show the found posts:

Rel3

Cheers

Patric

If you still get nothing, change this line in the code above

to

'value' => get_the_ID() ,

and see if you get anything.

Cheers

Patric

Would you put the ‘key’ for the relationship in the post currently or the one we are linking with?