Skip Navigation

[Resolved] Filter and/OR sort by Custom field in M2M relationship

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to sort and filter a View of an M2M post using a custom field on the intermediary relationship post.

Solution: The intermediary post type must be public because the View must query the intermediary post type. Then you can sort or filter by the custom fields of the intermediary post, and use the item attribute syntax to display information about the parent or child post type.

If you already created a relationship and didn’t specify a public intermediary post, you can edit the post type at Toolset > Post Types and change the status of the post type from draft to published. We plan to add the ability to sort and filter a View by fields of other related posts (but not by fields of hidden intermediary posts) soon after M2M stable version is released.

Relevant Documentation:
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/

This support ticket is created 6 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 3 voices.

Last updated by benA 5 years, 12 months ago.

Assisted by: Christian Cox.

Author
Posts
#729098
Screen Shot 2018-04-24 at 18.07.27.png

Hi!
Great work with the M2M relationship! I'm currently reworking our system to use this new feature and everything works fine but filter by and sorting by a custom field in the intermediary post.

The perfect example is the songs > track < album. I'd like to show only the 3 first tracks ( track number <= 3) and show it in the right order (sort by track number). Neither of those work out of the box.

The exact thing I did was product < product-auction > auctions] with a custom field (lotnum: Lot number, If 0, it's not lotted yet, otherwise should be shown in the auction view in the lot number order).
I have created the layout to show an Auction with a view to show the products. [wpv-view name="products-in-auction" ]
In the view, the Content Selection is "products", The query filter to "Post relationship filter" -> "Select posts in a Products Auctions relationship, that are a related to the current post in the loop.". This part works perfectly!

Now, to the problem: whenever i try to filter by 'lot number > 0' or order by 'intermediaryPost - Lot number' I get no results. Using the debug info, it seems the intermediary post is not on the query. Curiously enough I can print the 'lot number' info in the view without any problem by using [types field="lotnum" item="@product-auction.intermediary"][/types]
Is t

I'm using:
-wp-views.2.6-b4
-woocommerce.3.3.5
-woocommerce-views.2.7.6
-types.3.0-b2

With toolset-starter.1.4.2 theme

#730363

Hi, let me try to get the most up-to-date information about this. The new Post Relationship API has some information about querying with Intermediary post types, but I'm not sure if this can be accomplished in the GUI.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#full-documentation-of-the-query-argument

I will ask my 2nd tier support team for more information and update you shortly.

#734202

Okay the process is similar to the process in the old system. The intermediary post type must be public because the View must query the intermediary post type. Then you can sort or filter by the custom fields of the intermediary post, and use the item attribute syntax to display information about the parent or child post type:
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/

If you already created a relationship and didn’t specify a public intermediary post, you can edit the post type at Toolset > Post Types and change the status of the post type from draft to published. We plan to add the ability to sort and filter a View by fields of other related posts (but not by fields of hidden intermediary posts) soon after M2M stable version is released.

#804156

deleted message

#804157

Thanks, Christian, It worked!

Right now I have to create a view for my intermediary post (product-auction), to sort and filter it, and call a 'product' view on the loop. For instance, My intermediary view:

<wpv-loop>
            [wpv-view name="product-single"]
</wpv-loop>

And my Product view:

<wpv-loop>
           <li>
             [wpv-post-featured-image]
             [types field="lotnum" item="@product-auction.intermediary" ][/types]
             [wpv-post-link]
          </li>
</wpv-loop>

That ability to sort and filter by the intermediary post will bring a LOT of flexibility for sure ?

#1201324

Any update on this one? It would be very convenient to be able to filter/sort by fields in the intermediate/junction post type for M:N.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.