Last active
October 30, 2016 11:01
-
-
Save DanielFloeter/cdee81bbb6546c108b94e418b7169b90 to your computer and use it in GitHub Desktop.
Filter extension for the Event Post Widget: "Show only post from the current month and the current year"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Filter extension for the Event Post Widget: "Show only post from the current month and the current year" | |
* @url https://wordpress.org/plugins/event-post/ | |
*/ | |
function cpwp_add_extensions_panel( $widget, $instance ) { | |
$instance = wp_parse_args( ( array ) $instance, array( | |
'use_current_month_filter' => '' | |
) ); | |
$use_current_month_filter = $instance['use_current_month_filter']; | |
echo '<p>'. | |
'<label for="'.$widget->get_field_id('use_current_month_filter').'">'. | |
'<input type="checkbox" class="checkbox" id="'.$widget->get_field_id('use_current_month_filter').'" name="'.$widget->get_field_name('use_current_month_filter').'" '; | |
echo checked( (bool) $instance["use_current_month_filter"], true ).' />'; | |
echo _e( 'Use "Current month" filter extention','categorypostspro' ). | |
'</label>'. | |
'</p>'; | |
} | |
add_filter('cpwp_bottom_mysettings_panel','cpwp_add_extensions_panel', 10, 2); | |
function cpwp_get_posts_of_current_month ( $query, $widget, $instance ) { | |
if(isset($instance['use_current_month_filter'])&&$instance['use_current_month_filter']) { | |
$firstDay = date('Y-m-d',strtotime("first day of this month")); | |
$lastDay = date('Y-m-d',strtotime("last day of this month")); | |
$query['meta_query'] = array_merge( $query, array( | |
'relation' => 'AND', | |
array( | |
'key' => 'event_end', | |
'value' => $firstDay, | |
'compare' => '>=', | |
'type' => 'DATETIME' | |
), | |
array( | |
'key' => 'event_begin', | |
'value' => $lastDay, | |
'compare' => '<=', | |
'type' => 'DATETIME' | |
) | |
) | |
); | |
} | |
return $query; | |
} | |
add_filter( 'cpwp_query_args', 'cpwp_get_posts_of_current_month', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment