Skip Navigation

[Closed] Reordering a filtered toolset view before pagination happesn

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 10 months, 3 weeks ago.

Assisted by: Waqar.

Author
Posts
#2680439

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

#2680549

Hi Alex,

To troubleshoot this and suggest the next steps, I'll need to see exactly how this view is set up in the admin area.

Can you please share temporary admin login details, along with the link to the page with this view?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

The topic ‘[Closed] Reordering a filtered toolset view before pagination happesn’ is closed to new replies.