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:
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 ":
@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!
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:
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:
is just for displaying the selected posts. I am trying to figure out a meta query from this menu
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).
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â.
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.
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)
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.
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:
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).
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
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: