Skip Navigation

[Resolved] Custom Ordering using Views for Many to Many Relationships

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

Problem:

I have a custom post type 'content-panels' that has a many-to-many relationship with Pages.

I would like the order of the Content Panels to be different on a page-by-page basis, so I have created a numeric custom field ('position-of-content-panel-on-page') that is associated with the intermediary post type: 'content-panel-page'.

I have successfully set the order number for the Content Panels (different for each page). The problem I have is that I need Views to output the Content Panels in the order dictated by the same intermediate custom field.

Solution:

You need to setup a post view:

- Query posts of intermediary post type 'content-panel-page'

- Order the results by custom field 'position-of-content-panel-on-page'

- Filter by post type relationship between 'content-panels' and 'Pages'.

- In view's loop, display related "Content Panels" post information

Relevant Documentation:

https://toolset.com/course-lesson/displaying-related-posts/#displaying-one-related-item-parent

This support ticket is created 2 years, 9 months 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by BD 2 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#2288849

BD

I have a custom post type 'content-panels' that has a many-to-many relationship with Pages.

I would like the order of the Content Panels to be different on a page-by-page basis, so I have created a numeric custom field ('position-of-content-panel-on-page') that is associated with the intermediary post type: 'content-panel-page'.

I have successfully set the order number for the Content Panels (different for each page). The problem I have is that I need Views to output the Content Panels in the order dictated by the same intermediate custom field.

I created a View set to display 'content-panels' and selected the 'position-of-content-panel-on-page' as the orderby value. However, this returns no items. (I set the view to filter as follows: 'Select posts in a Content Panels Pages relationship that are a related to the current post in the loop.').

I then set the view to output both the Content Panels and the intermediary post type, but the intermediate post type showed up on the front end, not the content panels.

I am using PHP to output the view:
<?php echo do_shortcode ('[wpv-view name="Content Panels"]'); ?>

I have looked through all of the existing support tickets that I could find relating to this issue (particularly the two below), but I can't get it to work:

https://toolset.com/forums/topic/views-ordering-in-a-per-item-basis-in-many-to-many-relationship/
https://toolset.com/forums/topic/sorting-custom-view-query-by-intermediary-post-types-content-order-number/

Please could you assist? Thanks.

#2289125

Hello,

You need to setup a post view:
- Query posts of intermediary post type 'content-panel-page'
- Order the results by custom field 'position-of-content-panel-on-page'
- Filter by post type relationship between 'content-panels' and 'Pages'.
- In view's loop, display related "Content Panels" post information
https://toolset.com/course-lesson/displaying-related-posts/#displaying-one-related-item-parent

#2289401

BD
05.png
04.png
03.png
02.png
01.png

Thanks Luo, but I already tried this. I carried out your instructions as follows:

- Query posts of intermediary post type 'content-panel-page'
Done - see screenshot .

- Order the results by custom field 'position-of-content-panel-on-page'
Done - see screenshot

- Filter by post type relationship between 'content-panels' and 'Pages'.
Done - see screenshots

- In view's loop, display related "Content Panels" post information
Done - the titles that showed up were the intermediate post type titles - and no content panel information would display at all - see screenshot

The code in the loop editor is shown below. The Title shortcode returns the intermediate post type titles and the Content Panel Type shortcode (assigned to the content-panels post type) doesn't display anything on the front end.

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          <h2>[wpv-post-title]</h2>
          <h2>[types field='content-panel-type'][/types]</h2>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

The link you suggested led to a video about one-to-many using content blocks. I am using PHP to render a view for many-to-many.

Thanks for your ongoing assistance, Nick

#2291433

Please provide a test site with the same problem, fill below private message box with login details, also point out the problem page URL and view URLs, I can setup a demo for you.

#2292429

As I mentioned in first answer:

In view's loop, display related "Content Panels" post information

For example, edit the post view "Content Panels View", in section "Loop Editor", replace the shortcode [wpv-post-title] with:
[wpv-post-title item="@content-panel-page.parent"]

More help:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/
If your current post is in a relationship acting as an intermediary post, you can use the value of @{relationship-slug}.parent to obtain data from the related parent

#2292691

BD

Thanks for your assistance Luo and for expanding on your initial answer. Much appreciated - all working as expected now.