Last active
October 30, 2016 11:02
-
-
Save DanielFloeter/c0ead3b8068b345ff9ba70ed7a431833 to your computer and use it in GitHub Desktop.
Posts of current month - Filter extension
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: "Show only post from the current month and the current year" | |
/** | |
* Adds a checkbox to the My settings panel which can be checked/unchecked the 'Posts of current month' filter/hook | |
* | |
* @param widget, instance | |
* @return adds a ckeckbox to the form() | |
*/ | |
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); | |
/** | |
* Adds a filter/hook to get only post from the current month and year. | |
* | |
* @param query, widget, instance | |
* @return query | |
*/ | |
function cpwp_get_posts_of_current_month ( $query, $widget, $instance ) { | |
if( isset($instance['use_current_month_filter']) && $instance['use_current_month_filter'] ) { | |
$query = array_merge( $query, array( 'date_query' => array( | |
array( | |
'year' => date('Y'), | |
'month' => date('n') ) | |
) | |
) | |
); | |
} | |
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