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.
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
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
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.
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
Thanks for your assistance Luo and for expanding on your initial answer. Much appreciated - all working as expected now.