Quantcast
Channel: Toolset » All Posts
Viewing all articles
Browse latest Browse all 20145

Reply To: [Waiting for user confirmation] One-To-Many Relationship with additional custom field

$
0
0

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: http://wp-types.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!


Viewing all articles
Browse latest Browse all 20145

Trending Articles