Problem: I have 2 custom post types that are in a M2M relationship. I also have a View of post type A and a View of post type B. I would like to sort the Views by the number of related posts in the M2M relationship. In the View of post type A, I would like to sort the results by the number of post type B's each post type A is related to, and vice versa.
Solution: There is no built-in way to sort by a calculated value like this, so you would have to use a hidden custom field on post type A and post type B to store a calculated value. Update that value whenever one of the posts is created or updated, or whenever one of the post relationships is added or removed.
// update the number of workers custom field in the office post after association // and update the number of offices custom field in the worker post after association add_action( 'toolset_association_created', 'refresh_counts_after_association_created', 10, 5 ); function refresh_counts_after_association_created( $relationship_slug, $parent_id, $child_id, $intermediary_id, $association_id ) { $workers = toolset_get_related_posts($parent_id, 'office-worker', 'parent', 1000000,0, array(), 'post_id', 'child'); $count = sizeof($workers); update_post_meta($parent_id, '_worker-count', $count); $offices = toolset_get_related_posts($child_id, 'office-worker', 'child', 1000000,0, array(), 'post_id', 'parent'); $count = sizeof($offices); update_post_meta($child_id, '_office-count', $count); } // update the number of workers custom field in the office post before deleting association // and update the number of offices custom field in the worker post before deleting association add_action( 'toolset_before_association_delete', 'refresh_counts_before_association_delete', 10, 5 ); function refresh_counts_before_association_delete( $relationship_slug, $parent_id, $child_id, $intermediary_id, $association_id ) { $workers = toolset_get_related_posts($parent_id, 'office-worker', 'parent', 1000000,0, array(), 'post_id', 'child'); $count = sizeof($workers) - 1; update_post_meta($parent_id, '_worker-count', $count); $offices = toolset_get_related_posts($child_id, 'office-worker', 'child', 1000000,0, array(), 'post_id', 'parent'); $count = sizeof($offices) - 1; update_post_meta($child_id, '_office-count', $count); } // automatically update the worker count when saving an office post add_action( 'save_post', 'autoupdate_office', 15, 3 ); function autoupdate_office( $post_id, $post, $update ) { if ( $post->post_status == 'publish' && $post->post_type =='offices' ) { $workers = array(); $workers = toolset_get_related_posts($post_id, 'office-worker', 'parent', 1000000,0, array(), 'post_id', 'child'); $count = sizeof($workers); update_post_meta($post_id, '_worker-count', $count); } } // automatically update the office count when saving a worker post add_action( 'save_post', 'autoupdate_worker', 15, 3 ); function autoupdate_worker( $post_id, $post, $update ) { if ( $post->post_status == 'publish' && $post->post_type =='workers' ) { $offices = array(); $offices = toolset_get_related_posts($post_id, 'office-worker', 'child', 1000000,0, array(), 'post_id', 'parent'); $count = sizeof($offices); update_post_meta($post_id, '_office-count', $count); } } add_filter( 'wpv_filter_query', 'order_by_hidden_worker_count',199,3 ); function order_by_hidden_worker_count( $query_args, $views_settings, $view_id) { $view_ids = array( 1694 ); if (in_array($view_id, $view_ids)){ $args = array( 'meta_key' => '_worker-count', 'orderby' => 'meta_value', 'order' => 'DESC', ); return array_merge($query_args, $args); } return $query_args; } add_filter( 'wpv_filter_query', 'order_by_hidden_office_count',199,3 ); function order_by_hidden_office_count( $query_args, $views_settings, $view_id) { $view_ids = array( 12345 ); if (in_array($view_id, $view_ids)){ $args = array( 'meta_key' => '_office-count', 'orderby' => 'meta_value', 'order' => 'DESC', ); return array_merge($query_args, $args); } return $query_args; }
Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
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.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 14 replies, has 2 voices.
Last updated by 6 years, 3 months ago.
Assisted by: Christian Cox.