Skip Navigation

[Closed] One-To-Many Relationship with additional custom field

This support ticket is created 9 years, 11 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.

This topic contains 24 replies, has 3 voices.

Last updated by Juan 9 years, 8 months ago.

Assisted by: Bigul.

Author
Posts
#178512

I have a previous post you can see here: https://toolset.com/forums/topic/one-to-many-relationship-problem/

I was able to setup the relationship I needed using the custom taxonomy to related two custom post types to each other but now I need to add something and I can't find a way to do it.

I've got Courses and Video Series. The same Video Series can belong to more than one Course. Each Course contains many Video Series. This all works.

But now within each Course I want to be able to have a sort order for the Video Series. This sort order should be specific to that Course. So for example:

Course 1
-Video Series 1
-Video Series 2
-Video Series 3

Course 2
-Videos Series 2
-Video Series 3
-Video Series 1

In this example, Video Series 1 should have a sort order value of 1 for Course 1 but a sort order value of 3 for Course 2.

Is there any way to obtain this without having to have a separate view for each Course?

Thanks.

#179131

Bigul
Supporter

Dear Diego,

I will discuss your requirement with our lead developer and get back to you. May it will take 3-4 days before because he is in vacation. Please wait.

--
With Regards

Bigul

#179970

Bigul
Supporter

Dear Diego,

It can be done with some custom coding and some extra custom fields. I don't think it is very practical, because you would need to have a custom field for each Course, and store the sorting possition of each Video related to each Course there (and it can become a very big mess really fast), but theoretically it can be done. Then, when displaying the View, you can use the wpv_filter_query hook https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query to modify the sorting parameter being used.


With Regards

Bigul

#180795

Ok great. Can you give me more detail as to what exactly the custom field for each Course would be? What type of field should it be and what exactly what I store in that field for each Course? Is it some kind of comma separated list of the order of Video Series?

Thanks.

#181086

Bigul
Supporter

Dear Diego,

We will sort Views in Ascending/Descending order of Post fields(Title,ID,Date etc...) or Custom Fields. So better solution will be you can create a Custom Field Radio or Select for storing the Course Sort Order value. And based on that value you can sort Videos dynamically(By using custom coding and wpv_filter_query hook).


With Regards

Bigul

#181830

Hi again,

I just need a little further clarification. If I'm not creating a new custom field that is available to the Video Series custom type but instead it is available to the Courses custom type, then it would be something like a field called Video Series Sort Order and I make it a dropdown list. What exactly are the options that I put in to the dropdown list? Is it the list of video series in a comma separated string in the right order or something to that effect? Do you mean something like:

Option 1: "Series 1, Series 3, Series 12, Series 18, Series 2, Series 86"
Option 2: "Series 14, Series 15, Series 6, Series 37, Series 11"

etc. ? And instead of "Series 1" maybe it would be the actual post ID?

I hope I am misunderstanding or overcomplicating....because this is definitely not a very good solution...

#181882

Bigul
Supporter

Dear Diego,

Just for a confirmation, do you need custom sorting(Not ascending and descending) for each course ?

For example normally we will have only Ascending/Descending sort based on a field value. In which order and which field you want to sort ?


With Regards

Bigul

#182182

Hi,

I need the custom sorting on the Video Series that appear WITHIN a Course. I am using a custom taxonomy through toolset to relate Video Series to Courses. An individual Course will have many Video Series within it. But those Video Series can appear in more than one Course therefore it is a many-to-many relationship. I have this all setup and working. But the problem is, for each individual Course, when the Video Series are displayed for that Course, I want them displayed in a custom sort order that is specific to that Course.

I've still got a discover site setup that you can look at.

hidden link
In Course 1 you see Video Series appearing in the order 3, 2, 1.

hidden link
In Course 2 you see it also has Video Series 3 and 2 and they appear in the same order as Course 1.

I would like the Video Series in Course 2 to appear in a different order than Course 1. Not just Ascending or Descending post date though, a specific order for each Course.

#182242

Bigul
Supporter

Dear Diego,

You can consider a custom text field in Course for sorting purpose. For each Course you can enter the Video Post Ids in that text field with coma separation, something like this 145,156,167. Then you can do sorting on the basis of this values. But you have to do bunch of custom coding to achieve this.


With Regards

Bigul

#188764

Hello,

So I have been working on this in the manner that you suggested. I have the custom field in place with the comma separated list of video series post ids. I have added the wpv_filter_query_post_process filter so I can resort the posts after they have been loaded for my view. I am having a problem though within that filter method.

I have the code to get my custom field and convert it to an array. I also have the code to compare the post ids from that array to the post ids in the query passed in to the filter by wordpress/toolset.

The problem is that at the end of the filter I need to return the an object $query which is compatible to the passed in queried object as specified here: https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query_post_process/

The way I understand it is the queried object that is passed in contains an array of the posts for that view. What I'm doing is in my loop when I compare the ids in the order I want them to the order they are in the queried object, if they match I add that post to a new array. That is problem one. I don't know how to properly add the post object to my array.

Then at the end once I have a new array with the posts added in the order I want them, I need to add that array back to the object that was passed in to the filter in the first place and I don't know how to do that properly.

Here is the code I am talking about:

1. add_filter('wpv_filter_query_post_process', 'tpe_sort_video_series', 10, 2);
2. function tpe_sort_video_series($query, $view_settings) {
3. if ($view_settings['view_id'] == '9360') {
4. $order_string = types_render_field('video-series-sort-order', array('output'=>'html'));
5. $order_array = explode(",", $order_string);
6. $order_posts_array = array();
7. foreach($order_array as $id) {
8. while ($query->have_posts()) {
9. $query->the_post();
10. if(trim($id) == $query->post->ID) {
11. $order_posts_array[] = $query->the_post();
12. }
13. }
14. }
15. $query->posts = $order_posts_array;
16. }
17. return $query;
18. }

Line 11 is where I attempt to add the post to the new array. Line 15 is where I try to add my array of posts back to the queried object.

Can you help me to get these parts correct so I can make this work?

Thanks!

#189131

Bigul
Supporter

Dear Diego,

I will cross check your code and requirement once again and get back to you. Please wait.


With Regards

Bigul

#192620

Bigul
Supporter

Dear Diego,

Sorry for the delay, I was away for couple of days. I have received mail from your developer. You can't edit the theme files in discover-wp. Now I have access to your discover-wp site. Just for a confirmation you have only using one custom function for achieving this task (I mean one you have listed here https://toolset.com/forums/topic/one-to-many-relationship-with-additional-custom-field/#post-188764 ) ?


With Regards

Bigul

#196115

Yes that is correct, it is just one custom function for the wpv_filter_query_post_process. The code for that function is listed above but I can send it to you via email if necessary.

Thanks.

#196593

Bigul
Supporter

Dear Diego,

Okay fine. I will work on it and get back to you soon. Please wait.


With Regards

Bigul

#197093

Bigul
Supporter

Dear Diego,

I am still working on this. Last couple of days I haven't get much time to investigate the issue, because of some urgent task. Hope I can give you a favorable reply by tomorrow. Please wait.


With Regards

Bigul

The topic ‘[Closed] One-To-Many Relationship with additional custom field’ is closed to new replies.