Add a meta query to a query loop where posts are filtered by Metabox relation

Hi,

I have got a custom post type (book group) set up by Metabox with a relation to another custom post type (book group location).

I have a single post template setup, which I have applied to the “book group location” post type, and I want to display details of the book group location, and a list of future book groups that are linked to that location.

I’m using a query loop to display posts that are associated with the related book group location, using the “Type = MB Relationship” (as shown below).

image

This works perfectly to display the correct posts. However, I would also like to apply a post order and a meta query to further refine the list of posts, but these options (shown below) are not shown when I select “Type = MB Relationship”

image
image

Is it possible to do this in Bricks using the query loop? I’ve tried using nested loops without any success so far.

Any help would be gratefully received.

4 Likes

It would at least be useful to know if either:

  1. It is possible and I’m just doing it wrong, or
  2. It’s not possible with the query loop, and I’m barking up the wrong tree…

I can’t be the only one trying to do something like this, so if anyone has any insight on this I’d be really grateful.

Thanks

2 Likes

Bumping this because to this very day, I’m having this issue with Bricks not having native Metabox Relationship query functionality.

1 Like

I have the same issue. Can we, at least, modify the query with some hook from Bricks? @timmse

Found the solution with manual code:
→ I let the query loop to just “Posts” and altered it with this filter: Filter: bricks/posts/query_vars – Bricks Academy

Any news about this? How to show the options (order etc.) when you use MB Relationship?

Same thing with ACF PRO and Relationships functionality. When you select the ACF Relationship, all the functionality for Order by etc. is lost.

Filter: bricks/query/result – Bricks Academy.

Exactly, as Bricks-Guru Sridhar pointed out, you can use the filter to prepare the query like you want…

I know that many prefer “no-coding” instead, but I love the endless possibilities that this filter gives.

Cheers

Patric

Thanks but people with no coding skills must have access somehow via the builder’s UI to change these stuff. I don’t know why it’s so hard implement this. There are many posts about the same thing from many months ago.

Imagine you have a Single page template for a Book Author and want to display his books sorted by Name, Price, Year etc. Good luck doing that via code and explain that to your customer how to change it…

I hear you.

But relationship queries are not just simple queries, therefore the implementation is more complicated. Also, I don’t think you really want your customer to change query parameters either.

Anyway, you can of course make a first query of the post type and its posts, and within this query make a second query where you check in the meta query parameters for the post_id.

Or when you are in a single post template, you can make a query in that template with the meta query parameter for the post_id.

Both will then find all related posts that have the post_id of the post from the first query or from the single post template.

Then you have all sorting etc. query parameters.

See this for more info:

The meta key has to be the name of the relationship field and the meta value must be “{post_id}” including the quotes!

Cheers

Patric

Thanks for the info.

It works but only for ACF.

In metabox the way relationships work is that metabox creates a seperate table and stores there the related ids of the related posts. That is the best approach for scalability. It’s like you are using a foreign key table if you know how relational database tables work.

ACF on the other hand stores the relationships in postmeta table and it stores them serialized. Very bad practice but works with the querying thing of bricks.