Startseite › Toolset Professional Support › [Warten auf die Bestätigung des Benutzers] Custom code to make the The Calendar plugin compatible
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 |
|---|---|---|---|---|---|---|
| - | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
| - | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Zeitzone des Unterstützers: Asia/Kolkata (GMT+05:30)
Dieses Thema enthält 12 Antworten, hat 1 Stimme.
Zuletzt aktualisiert von Minesh vor 4 days, 4 hours.
Assistiert von: Minesh.
I need custom code to make the The Calendar plugin compatible. Some time ago, you created code for me to solve a compatibility issue between Toolset and The Calendar. Now I have a new issue: I would like to change the order of the list in the different Views based on the “Esdeveniment” field.
I would like to know whether it is possible to modify the code so that, in each View, I can choose a different sort order: in one View from the nearest date to the farthest, and in another View the reverse order.
Below is the code you created for me. Could you modify it so that, in each View, I can change the order of the listing using a custom field called “Esdeveniments”.
<?php
/**
* New custom code snippet (replace this with snippet description).
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
function func_remove_unwanted_posts( $query_args ,$view_settings, $view_id ) {
global $post;
global $current_user;
if ( $view_id == 3003 ) {
$query_args['suppress_filters'] = 1;
}
return $query_args;
}
add_filter( 'wpv_filter_query', 'func_remove_unwanted_posts', 999, 3);
add_action('pre_get_posts', 'func_tribe_events_date_filter_adjust',99,1);
function func_tribe_events_date_filter_adjust( $query ) {
global $WP_Views;
$target_view_ids = array(5575,3003,5429);
if(in_array($WP_Views->current_view,$target_view_ids)){
/// if($WP_Views->current_view == 5575){
$meta_query = $query->get('meta_query') ?: [];
$remove_keys = ['tec_event_start_date', 'tec_event_end_date'];
// Remove unwanted keys
foreach ($remove_keys as $key) {
if ( isset($meta_query[$key]) ) {
unset($meta_query[$key]);
}
}
$query->set('meta_query', $meta_query);
}
return $query;
}
add_filter( 'wpv_filter_query_post_process', 'func_sort_event_date_within_view_result', 10, 3 );
function func_sort_event_date_within_view_result( $query, $view_settings, $view_id ) {
$target_view_ids = array(5575,3003,5429);
if (in_array($view_id,$target_view_ids) && !empty($query->posts)) {
usort($query->posts, function($a, $b) {
$date_a = get_post_meta($a->ID, 'wpcf-data-esdeveniment', true);
$date_b = get_post_meta($b->ID, 'wpcf-data-esdeveniment', true);
$time_a = strtotime($date_a) ?: 0;
$time_b = strtotime($date_b) ?: 0;
return $time_b - $time_a; // DESC
});
$query->found_posts = count($query->posts);
$query->post_count = count($query->posts);
}
return $query;
}
Hello. Thank you for contacting the Toolset support.
I need to check how the view is configured and can you please tell me with that view you want to display posts in DESC order and with what view you want to display posts in ASC.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Can you pleae confirm the following links are working as expected:
- versteckter Link
- versteckter Link
I've adjusted the code added to "Custom Code" setion with code snippet "toolset-custom-code" as given under:
=> versteckter Link
add_filter( 'wpv_filter_query_post_process', 'func_sort_event_date_within_view_result', 10, 3 );
function func_sort_event_date_within_view_result( $query, $view_settings, $view_id ) {
$target_view_ids = array(5429);
$target_view_ids_recent_oldest = array(5575,3003);
if (in_array($view_id,$target_view_ids) && !empty($query->posts)) {
usort($query->posts, function($a, $b) {
$date_a = get_post_meta($a->ID, 'wpcf-data-esdeveniment', true);
$date_b = get_post_meta($b->ID, 'wpcf-data-esdeveniment', true);
$time_a = strtotime($date_a) ?: 0;
$time_b = strtotime($date_b) ?: 0;
if (in_array($view_id, $target_view_ids)) {
return $time_b - $time_a; // DESC
}
return $time_b <=> $time_a; // DESC (recent → oldest)
});
$query->found_posts = count($query->posts);
$query->post_count = count($query->posts);
}
return $query;
}
Hello, the changes are not noticeable.
On the conferences page, the first view is correct, but in the second section (past events), the most recent event should appear first. In this case, it should be the event dated March 24, 2026, instead of the one from November 5, 2024.
The same issue occurs with the monographic sessions: the first view is correct, but in the second list, the order should be updated so that the most recent event (March 23, 2026) appears first.
On the homepage, there are no changes in the order. If you look at the dates, they are not in sequence. The last two slides (13 and 14) should be positioned earlier, as their dates are more recent.
Rafa corts
Can you please check now:
- versteckter Link
- versteckter Link
I've adjusted the code added to "Custom Code" section as given under:
=> versteckter Link
add_filter('wpv_filter_query_post_process', 'func_sort_event_date_within_view_result', 10, 3);
function func_sort_event_date_within_view_result($query, $view_settings, $view_id) {
$target_view_ids_desc = array(5429); // DESC
$target_view_ids_recent_oldest = array(5575,3003); // recent → oldest
if (
(in_array($view_id, $target_view_ids_desc) || in_array($view_id, $target_view_ids_recent_oldest))
&& !empty($query->posts)
) {
usort($query->posts, function($a, $b) use ($view_id, $target_view_ids_desc) {
$time_a = (int) get_post_meta($a->ID, 'wpcf-data-esdeveniment', true);
$time_b = (int) get_post_meta($b->ID, 'wpcf-data-esdeveniment', true);
if (in_array($view_id, $target_view_ids_desc)) {
return $time_b <=> $time_a;
}
return $time_b <=> $time_a;
});
$query->found_posts = count($query->posts);
$query->post_count = count($query->posts);
}
return $query;
}
Hello,
The other views are now working perfectly and follow the correct order. The only thing left is to properly sort the Home page view "Slider2" (ID 71).
As you can see, it is currently being ordered first by category and then by date. However, all events should be mixed together and sorted by the custom field "wpcf-data-esdeveniment", from most recent to oldest.
Thank you for your support.
Best regards,
Rafa Corts
Can you please check homepage: versteckter Link
is it in correct order? if not - what posts you want to see in what order?
Hello,
The last two sliders dated May 6, 2026 and April 15, 2026 cannot be the most recent ones, since for example Slider 11 is dated June 16 and should be the latest.
Sorry but May 6, 2026 and April 15, 2026 are the coming dates so those are not latest or you mean those date sliders should display first in ASC order and then other future date, I mean you want to display the posts that belongs to future date in ASC order or DESC?
ASC will display april, may, june
DESC will display june, may, april
Hello, I would like the order to be set to "ASC". However, as you can see, the current sorting is based on the creation time of each event rather than the event date.
If you look closely, the last two sliders are out of order — these are the ones I created most recently.
Slider 01: April 14, 2026 Slider 02: April 14, 2026 Slider 03: April 21, 2026 Slider 04: April 28, 2026 Slider 05: May 5, 2026 Slider 06: May 12, 2026 Slider 07: May 19, 2026 Slider 08: May 26, 2026 Slider 09: June 2, 2026 Slider 10: June 9, 2026 Slider 11: June 16, 2026 Slider 12: April 15, 2026 Slider 13: May 6, 2026
All sliders should be sorted by event date, from the earliest to the latest.
Saludos.
Rafa corts
When you say event date - do you mean you want to use the following date custom field "wpcf-data-esdeveniment"?
Hello, yes that’s correct — I want to sort by the "wpcf-data-esdeveniment" field.
I've adjusted the code to "Custom Code" section as given under:
add_action('pre_get_posts', 'func_tribe_events_date_filter_adjust',99,1);
function func_tribe_events_date_filter_adjust( $query ) {
global $WP_Views;
$target_view_ids = array(5575,3003,5429,71);
if(in_array($WP_Views->current_view,$target_view_ids)){
/// if($WP_Views->current_view == 5575){
$meta_query = $query->get('meta_query') ?: [];
$remove_keys = ['tec_event_start_date', 'tec_event_end_date'];
// Remove unwanted keys
foreach ($remove_keys as $key) {
if ( isset($meta_query[$key]) ) {
unset($meta_query[$key]);
}
}
if($WP_Views->current_view == 71){
$query->set('suppress_filters', 1);
}
$query->set('meta_query', $meta_query);
}
return $query;
}
add_filter('wpv_filter_query_post_process', 'func_sort_event_date_within_view_result', 30, 3);
function func_sort_event_date_within_view_result($query, $view_settings, $view_id) {
$target_view_ids_desc = array(5429); // DESC
$target_view_ids_recent_oldest = array(5575,3003,71); // recent → oldest
if (
(in_array($view_id, $target_view_ids_desc) || in_array($view_id, $target_view_ids_recent_oldest))
&& !empty($query->posts)
) {
usort($query->posts, function($a, $b) use ($view_id, $target_view_ids_desc) {
$time_a = (int) get_post_meta($a->ID, 'wpcf-data-esdeveniment', true);
$time_b = (int) get_post_meta($b->ID, 'wpcf-data-esdeveniment', true);
if (in_array($view_id, $target_view_ids_desc)) {
return $time_b <=> $time_a;
}
return $time_b <=> $time_a;
});
$query->found_posts = count($query->posts);
$query->post_count = count($query->posts);
}
return $query;
}
Can you please confirm it works as expected now.