I tried getting all snippets of code and creating the following function:
/**
* Sort the elements by role name, or customised catgory order
*
* @param mixed $sort_list
* @param mixed $sort_view_id
* @param mixed $orderBy
* @param mixed $query
*/
function sort_by_role_name($post_order_list, $sort_view_id, $orderBy, $query)
{
//Change to required taxonomy
$_apply_for_taxonomy = "role";
if (count($post_order_list) > 1)
return $post_order_list;
//check if is archive or a taxonomy view
if (count($query->tax_query->queries) > 1)
return $post_order_list;
//avoid endless loop in case using this function on multiple level terms
global $_SCN_in_the_loop;
if ($_SCN_in_the_loop)
return $post_order_list;
$_is_archive = FALSE;
$_is_term = FALSE;
if (count($query->tax_query->queries) > 0) {
//ensure there's a single term
reset($query->tax_query->queries);
$tax = current($query->tax_query->queries);
if (is_array($tax['terms']) && count($tax['terms']) > 1)
return $post_order_list;
$_is_term = TRUE;
} else
$_is_archive = TRUE;
//retrive the terms
$args = array(
'taxonomy' => $_apply_for_taxonomy,
'orderby' => 'name',
);
if (count($query->tax_query->queries) > 0) {
//ensure there's a single term
reset($query->tax_query->queries);
$tax = current($query->tax_query->queries);
if (is_array($tax['terms']) && count($tax['terms']) > 1)
return $post_order_list;
$_is_term = TRUE;
} else
$_is_archive = TRUE;
//retrive the terms
$args = array(
'taxonomy' => $_apply_for_taxonomy,
'orderby' => 'name',
);
if ($_is_term) {
//retrieve a list of existing terms
reset($query->tax_query->queries);
$tax = current($query->tax_query->queries);
if (is_array($tax['terms'])) {
reset($tax['terms']);
$term_id = current($tax['terms']);
} else
$term_id = $tax['terms'];
if ($tax['field'] == 'slug' || $tax['field'] == 'name') {
$term_data = get_term_by($tax['field'], $term_id, $_apply_for_taxonomy);
$term_id = $term_data->term_id;
}
$args['child_of'] = $term_id;
}
$terms = get_terms($args);
//retrieve a list of items for each of the terms
if (count($terms) < 1) return $post_order_list;
$_SCN_in_the_loop = TRUE;
global $APTO;
$sort_view_post = get_post($sort_view_id);
$sort_list_settings = $APTO->functions->get_sort_settings($sort_view_post->post_parent);
$posts_list = array();
foreach ($terms as $term) {
$args = array(
'post_type' => $sort_list_settings['_rules']['post_type'][0],
'posts_per_page' => -1,
'fields' => 'ids',
'orderby' => 'menu_order',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $_apply_for_taxonomy,
'field' => 'id',
'terms' => array($term->term_id),
),
),
);
$term_posts_query = new WP_Query($args);
$posts_list = array_merge($posts_list, array_values($term_posts_query->posts));
}
$_SCN_in_the_loop = FALSE;
return $posts_list;
}
But it doesn’t work when setting Order By to Custom Function:
View post on imgur.com
My custom query is set like this and I need to order by role order:
View post on imgur.com
Thank you for your help in advance!