This thread is resolved. Here is a description of the problem and solution.
Problem:
In a many-to-many relationship the client wants to display related posts, sorted by a field on the related posts.
Solution:
The current implementation of many-to-many relationships involves creating an intermediate post type, and then creating a View which returns the intermediate posts with a filter for the parents on one side, while using the id attribute to display content from the parent on the other side.
That View can only filter by fields present on the intermediate post type itself, which means duplicating the field in question from the parent on the intermediate posts.
This will become redundant with the switch to Types 3.0, when it will eventually be possible to filter by fields of related posts.
This support ticket is created 6 years, 10 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.
Our next available supporter will start replying to tickets in about 0.99 hours from now. Thank you for your understanding.
Hello, I created 3 custom post type 'concert' 'artist' and 'participants' which are a post-relationship.
I try to display in the single page 'artist' concerts that are only in reference to this artist.
It does not work anymore when I want to sort by date of concert. can you help me on this ?
Here is the page where the view should appear : hidden link
This de code of the view
Your View queries participant posts, and so you can only filter by fields of participants. A participant post doesn't know what the concert date is.
As you are probably aware, post relationships are undergoing a major overhaul, and there is a feature request to make it possible to filter by fields of the parent, which would make what you are trying to do possible.
I don't think it will be included in the initial stable release, but my understanding from the developers is that it should be added within one or two subsequent releases.
With the current versions, though, it is not possible.
Thank you, this is exactly the same case as me in the link "Display events at which an artist appears" and I use this method.
Except that I have a concert date field and it is apparently impossible to sort by concert date and only show future concerts. It seemed to me something very simple, too bad, I use Toolset for some time and this is the first time I can achieve what I want.
Thank you for your help...
Well, currently the only way to output the concerts that relate to the artist being displayed is via a View which queries the intermediate participant posts, and you can only filter by fields of participant posts. The concert date is not a field of participants and so cannot be used.
There are two possible solutions.
One is to duplicate the concert date field on the participant posts. You can then not only use the concert date field to filter the results (only show future concerts) but to order them, too (show upcoming concerts in date order). To save having to manually enter the date in the participant post you could add a little PHP using the save_post hook (https://codex.wordpress.org/Plugin_API/Action_Reference/save_post) so that when a participant post is saved you get the date from the parent concert post and add it to the post meta of the recently saved participant post.
The second does not require any custom code but is more limited. In the Loop Output section of your existing participants View you can wrap the output in a wpv-conditional tag and add a test for the date field, using the same id="$concert" technique to specify that the date field belongs to the parent concert post. For non-matching posts nothing would be output, so you would only see the future concerts. You wouldn't be able to order by the concert dates, though, only by properties of the participant posts.
If you try one of these and need more help do let me know.
I still do not have the solution to sort by date of concert in a many-to-many relationship, it calls into question all my site that works with toolset, my client needs to sort the concert by date of concert in a single page artist, that me seems something basic, is there another solution ?
The many-to-many relationship looks like that:
Sorry for the delay getting back to you, it was a holiday weekend here in Ireland.
If you require being able to sort the connected concerts by concert date then with the current implementation the only option would the first option I described in my previous post, where you duplicate the concert date field on the intermediate participant posts. Then your View which queries participant posts (to output fields from the parent concert posts) can be filtered and sorted by the concert date.
In terms of an easier workflow you can use the save_post hook to automatically copy the concert date field from the parent concert post when a participant post is saved, rather than have to add it manually a second time.
This will be possible natively within Views when the work on post relationships is complete, but for now you would need to adopt the above methodology.
You can use the following code to duplicate the field from a parent when a child post is saved/updated:
add_action( 'save_post', 'tssupp_duplicate_parent_field', 101, 3 );
function tssupp_duplicate_parent_field( $post_id, $post, $update ){
$child_slug = 'participant';
$parent_slug = 'concert';
$parent_field_slug = 'concert-date';
// is it a child post being updated?
if ( $child_slug == $post->post_type ) {
// get the ID of the parent post
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_' . $parent_slug . '_id', true );
if ( !empty( $parent_id ) ) {
// get the parent field
$parent_field_value = get_post_meta( $parent_id, 'wpcf-' . $parent_field_slug, true );
// duplicate the parent field on the child post
update_post_meta( $post_id, 'wpcf-' . $parent_field_slug, $parent_field_value );
}
}
}
You will need to edit the slugs.
This won't affect your existing posts.
For that you will need to run some similar code one-time only. You can add the above code to your theme's functions.php file, or using a plugin such as Code Snippets.
I would recommend Code Snippets for the second code sample below, because it has a feature where you can add code and just run it once, which is perfect for when you need to update content in the database.
function tssupp_duplicate_parent_fields_once (){
$child_slug = 'participant';
$parent_slug = 'concert';
$parent_field_slug = 'concert-date';
// Get all of the child posts
$args = array(
'post_type' => $child_slug,
'numberposts' => -1,
'post_status' => 'publish'
);
$child_posts = get_posts( $args );
if ( !empty( $child_posts ) ) {
foreach ($child_posts as $child_post) {
// get the ID of the parent post
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_' . $parent_slug . '_id', true );
if ( !empty( $parent_id ) ) {
// get the parent field
$parent_field_value = get_post_meta( $parent_id, 'wpcf-' . $parent_field_slug, true );
// duplicate the parent field on the child post
update_post_meta( $post_id, 'wpcf-' . $parent_field_slug, $parent_field_value );
}
}
}
}
Hello Nigel,
I just tried your method but when I try to sort by concert date, nothing is displayed, is there anything else to do ?
How do i know if the date concert field has been duplicated ?
Thanks for your support .
Olivier Foguenne
You can look in the database to see whether the concert date field has been duplicated.
Get the ID of one of the participant posts and then using phpMyAdmin look at the wp_postmeta table and search for the post meta entries for that post ID. You can do the same using the ARI Adminer plugin directly in your WordPress backend.
If you like I can do that for you, but I would need access, so let me mark the next reply as private in case you need to share credentials with me.