Hi there,
I want to reorder my toolset view with a dynamic source. The value for reordering is the "number of found custom taxonomies with at least 1 matched term after filter".
I nearly made it but can't combine it with the pagination. If i hook in earlier (wpv_filter_query) it crashes my site
Her is my code so far:
add_filter('wpv_filter_query_post_process', 'custom_filter_after_query', 10, 2);
function custom_filter_after_query($query, $view_settings) {
// Check if the view ID is the one you are interested in
if (isset($view_settings['view_id']) && $view_settings['view_id'] == 3803) {
// Array of your taxonomies
$taxonomies = array('praktikumsart', 'beruf', 'berufsfeld', 'faehigkeit', 'interesse', 'sprache');
// Get the current posts
$posts = $query->posts;
// Initialize an array to store post IDs and their corresponding taxonomy counts
$post_taxonomy_counts = array();
// Loop through each post
foreach ($posts as $post) {
$taxonomy_count = 0; // Initialize the taxonomy count for each post
// Check each taxonomy for terms
foreach ($taxonomies as $taxonomy) {
$terms = get_the_terms($post->ID, $taxonomy);
if (!empty($terms)) {
// Assuming you can retrieve the selected terms from the URL parameters
$selected_terms = isset($_GET['pf-' . $taxonomy]) ? (array)$_GET['pf-' . $taxonomy] : array();
// Check if the current post's terms match the selected terms in the filter
foreach ($terms as $term) {
if (in_array($term->slug, $selected_terms)) {
$taxonomy_count++; // Increment count if terms match
break; // Break the loop once a match is found for the current taxonomy
}
}
}
}
// Update the custom field with the taxonomy count for each post
update_post_meta($post->ID, 'wpcf-sortierung-tax', $taxonomy_count);
// Store post ID and corresponding taxonomy count in the array
$post_taxonomy_counts[$post->ID] = $taxonomy_count;
}
// Sort the posts array based on taxonomy counts in descending order
usort($query->posts, function ($a, $b) use ($post_taxonomy_counts) {
return $post_taxonomy_counts[$b->ID] - $post_taxonomy_counts[$a->ID];
});
}
return $query;
}
so this works. but it is reordering each page for itself. this doesnt help. i need the reordering and then the pagination logic. do you have a clue how to solve this problem or another approach?
Thx, Alex