[Fermé] One-To-Many Relationship with additional custom field
This support ticket is created Il y a 9 années et 9 mois. 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.
Really sorry for the delay. We are still working on the issue and I have created a demo installation for you. I will update you the status in couple of days.
I was away last few days, because of my wife's pregnancy.
Today I spent some time on your requirement and tried to achieve the task in my local installation. Now I am facing an issue to sort the listing. Please correct me if I am wrong.
You have three Post Types. Default Posts and two custom Post Types Courses and Video series. Posts is a child of Video Series. And you are relating Course and Video Series through Course Relations flat taxonomy.
You are listing Video Series in Course post like hidden link (Through Course Relation data) and you are also listing the Child Posts of Video Series there. You need to achieve custom sorting for Child Posts of Video Series listed in Course Posts, am I right ?
Really sorry, it makes more complications in your requirement. Earlier we thought you have only two post types Course and Video Series. But actually you need custom sorting for child on the basis of Parent post's relative post.
It will be very hard to achieve. Because sorting order for Post will be varies on the basis of Course and not on the basis of Video series. Because Post is the child of Video Series and not the child of Course.
I'm not quite sure I understood your last message and maybe I answered your previous question incorrectly. I am not concerned about the sorting of Posts within Video Series based on Courses. I am just trying to sort the Video Series themselves within Courses. The child Posts within Video Series are already sorted by Video Series that that sorting always stays the same. I just need to be able to have different sorting of Video Series for each Course.
This is Juan, lead Views developer. I can confirm you that we have a solution for this 🙂 First, let me set some names:
* Your View listing Video Series has an ID of XXX and a title of "Course - Video Series"
* The custom field in every Course where you are storing the comma separated and sorted list of Video Series is named "wpcf-video-series-sort-order"
So, our plan is the following:
1. We are going to add this custom field content to the [wpv-view name="Course - Video Series"] shortcode (used in each Course) as an attribute named "sorting", like this:
[wpv-view name="Course - Video Series" sorting="[wpv-post-field name='wpcf-video-series-sort-order']"]
2. Then, when processing that wpv-view shortcode and before running its query, we are going to take that custom field value and force the query to sort its results following that value.
3. Profit 🙂
To do so, we are going to add this filter to our functions.php file. Remember to adjust XXX to the actual View ID:
// hook the filter to modify the Views query
add_filter( 'wpv_filter_query', 'prefix_sort_by_my_custom_field', 999, 3 );
function prefix_sort_by_my_custom_field( $query_args, $view_settings, $view_id ) {
if ( $view_id == XXX ) { // replace XXX with the actual View ID, so this filter only affects this one
global $WP_Views;
$attr = $WP_Views->get_view_shortcodes_attributes(); // get the attributes from the wpv-view shortcode
if ( isset( $attr['sorting' ] ) && !empty( $attr['sorting'] ) ) {// if this wpv-view shortcode has a sorting attribute, do this
$posts_in = array_map( 'trim', explode( ',', $attr['sorting'] ) );// clean the list of IDs used to sort
if ( isset( $query_args['post__in'] ) ) {// play nicely with possible existing settings for the query
$query_args['post__in'] = array_intersect( $posts_in, $query_args['post__in'] );
} else {
$query_args['post__in'] = $posts_in;
}
if ( isset( $query_args['post__not_in'] ) ) {
$query_args['post__in'] = array_diff( $query_args['post__in'], $query_args['post__not_in'] );
}
$query_args['orderby'] = 'post__in';// make the View return elements following the custom field value
}
}
return $query_args;
}