Last active
April 14, 2016 09:08
-
-
Save marushu/cddeb17ed9b1371e8794890643bf7d5c to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Set event month filter at Event post type admin column | |
*/ | |
add_filter( 'query_vars', 'goto_calendar_register_query_vars' ); | |
function goto_calendar_register_query_vars( $qvars ) { | |
//Add these query variables | |
$qvars[] = 'custom_month'; | |
return $qvars; | |
} | |
/** | |
* Add event_date to admin column. | |
*/ | |
// add event date field admin column | |
add_filter( 'manage_event_posts_columns', 'add_event_date_admin_columns_head' ); | |
function add_event_date_admin_columns_head( $defaults ) { | |
// Remove default post date column | |
unset($defaults['date']); | |
$defaults[ 'event_date' ] = 'Event Day'; | |
return $defaults; | |
} | |
add_action( 'manage_event_posts_custom_column', 'add_event_date_admin_column_content', 10, 2 ); | |
function add_event_date_admin_column_content( $column_name, $post_id ) { | |
global $post; | |
if ( $column_name == 'event_date' ) { | |
$event_date = get_post_meta( $post_id, 'event_date', true ); | |
if( ! empty( $event_date ) ) { | |
echo date( 'Y/m/d', strtotime( $event_date ) ); | |
} else { | |
echo ''; | |
} | |
} | |
} | |
/** | |
* Remove date filter at Event post type admin column. | |
*/ | |
add_filter( 'disable_months_dropdown' , 'custom_disable_months_dropdown' , 10 , 2 ); | |
function custom_disable_months_dropdown( $false , $post_type ) { | |
$disable_months_dropdown = $false; | |
$disable_post_types = array( 'event' ); | |
if( in_array( $post_type , $disable_post_types ) ) { | |
$disable_months_dropdown = true; | |
} | |
return $disable_months_dropdown; | |
} | |
/** | |
* Add post meta date filter to even post type. | |
*/ | |
add_action( 'restrict_manage_posts', 'goto_calendar_restrict_posts_by_metavalue' ); | |
function goto_calendar_restrict_posts_by_metavalue() { | |
global $typenow; | |
$months = goto_calendar_get_months(); | |
if ($typenow == 'event') { | |
$selected = get_query_var('custom_month'); | |
$output = '<select name="custom_month" class="postform">' . "\n"; | |
$output .= '<option ' . selected( $selected, 0, false ).' value="">' . __( 'Show All', 'some_textdomain' ) . '</option>'; | |
if ( ! empty( $months ) ) { | |
foreach ( $months as $month ) { | |
$value = esc_attr( $month->year . '/' . $month->month ); | |
$month_dt = new DateTime( $month->year . '-' . $month->month . '-01' ); | |
$output .= '<option value="' . $value . '"' . selected( $selected, $value, false ) . '>' . $month_dt->format( 'Y年m月' ) . '</option>'; | |
} | |
} | |
$output .= '</select>' . "\n"; | |
echo $output; | |
} | |
} | |
add_action( 'pre_get_posts', 'goto_calendar_pre_get_posts' ); | |
function goto_calendar_pre_get_posts( $query ) { | |
//Only alter query if custom variable is set. | |
$month_str = $query->get( 'custom_month' ); | |
if( !empty( $month_str ) ){ | |
$meta_query = $query->get( 'meta_query' ); | |
if( empty( $meta_query ) ) | |
$meta_query = array(); | |
$month = new DateTime($month_str.'/01'); | |
$meta_query[] = array( | |
'key' => 'event_date', | |
'value' => array( $month->format( 'Ymd' ), $month->format( 'Ymt' ) ), | |
'compare' => 'BETWEEN', | |
); | |
$query->set( 'meta_query', $meta_query ); | |
} | |
} | |
function goto_calendar_get_months(){ | |
global $wpdb; | |
$months = wp_cache_get( 'goto_calendar_get_months' ); | |
if ( false === $months ) { | |
$query = "SELECT YEAR( meta_value ) AS `year`, MONTH( meta_value ) AS `month`, count( post_id ) as posts | |
FROM $wpdb->postmeta | |
WHERE meta_key = 'event_date' | |
AND post_id IN ( SELECT ID FROM $wpdb->posts | |
WHERE post_type = 'event' | |
AND post_status != 'trash' ) | |
GROUP BY YEAR( meta_value ), MONTH( meta_value ) ORDER BY meta_value DESC"; | |
$months = $wpdb->get_results($query); | |
wp_cache_set( 'goto_calendar_get_months', $months ); | |
} | |
return $months; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment