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;
}