Skip Navigation

[Fermé] One-To-Many Relationship with additional custom field

This support ticket is created Il y a 8 années et 7 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.

This topic contains 24 réponses, has 3 voix.

Last updated by Juan Il y a 8 années et 4 mois.

Assigned support staff: Bigul.

Auteur
Publications
#198547

Bigul
Supporter

Dear Diego,

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.


With Regards

Bigul

#199056

Bigul
Supporter

Dear Diego,

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 ?


With Regards

Bigul

#200503

Yes exactly right, that is what I need to do.

#201110

Bigul
Supporter

Dear Diego,

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.

Please correct me if I am wrong.


With Regards

Bigul

#206853

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.

#207362

Bigul
Supporter
2014-03-26_1626.png

Dear Diego,

Thank you for the clarifications. Just for a confirmation please check attached image and let me know your feedback.


With Regards

Bigul

#207648

Yes that is what I would be looking to do. Each video series should be able to appear in a different order in each course.

#207795

Bigul
Supporter

Dear Diego,

I am working on it. Hope I can give you a favorable reply on tomorrow. Please wait.


With Regards

Bigul

#208696

Bigul
Supporter

Dear Diego,

I am trying to solve the issue with the help of our lead developer and get back you as soon as possible. Most probably by evening. Please wait.


With Regards

Bigul

#208742

Juan
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Dear Diego

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;
}

This should do the trick 🙂

Let me know if you need help implementing this.

Regards,
Juan de Paco.

Le sujet ‘[Fermé] One-To-Many Relationship with additional custom field’ est fermé à de nouvelles réponses.