Skip to content

Instantly share code, notes, and snippets.

@nguyenvanduocit
Created June 2, 2015 03:35
Show Gist options
  • Save nguyenvanduocit/44c14c77fda32263c702 to your computer and use it in GitHub Desktop.
Save nguyenvanduocit/44c14c77fda32263c702 to your computer and use it in GitHub Desktop.
Create filter for listpost in admin
<?php
/**
* Summary
* Description.
*
* @since 0.9.0
* @package
* @subpackage
* @author nguyenvanduocit
*/
class ET_Filter {
private static $instance;
public static function get_instance() {
if ( NULL == self::$instance ) {
self::$instance = new ET_Filter();
}
return self::$instance;
}
public function init() {
add_action( 'restrict_manage_posts', array( $this, 'add_filter' ) );
add_action( 'pre_get_posts', array( $this, 'posts_filter' ) );
}
/**
* Add filter control.
*
* @since 0.9.0
* @see
* @return void
* @author nguyenvanduocit
*/
public function add_filter() {
if ( isset( $_GET[ 'post_type' ] ) ) {
$type = $_GET[ 'post_type' ];
}
//only add filter to post type you want
if ( 'thread' === $type ):
//change this to the list of values you want to show
//in 'label' => 'value' format
$values = array(
'all' => 'Reply filter',
'wait_customer_reply' => 'Wait for customer',
'wait_mod_reply' => 'Wait for mod',
'not_reply' => 'not reply'
);
?>
<select name="reply_filter">
<?php
$current_v = isset( $_GET[ 'reply_filter' ] ) ? $_GET[ 'reply_filter' ] : '';
foreach ( $values as $value => $label ):
printf
(
'<option value="%s"%s>%s</option>',
$value,
$value == $current_v ? ' selected="selected"' : '',
$label
);
endforeach;
?>
</select>
<?php
endif;
}
public function posts_filter( $query ) {
global $pagenow;
$type = 'post';
if ( isset( $_GET[ 'post_type' ] ) ) {
$type = $_GET[ 'post_type' ];
}
if ( 'thread' === $type && is_admin() && 'edit.php' === $pagenow && isset( $_GET[ 'reply_filter' ] ) && $_GET[ 'reply_filter' ] !== '' ) {
if ( ! empty( $_GET[ 'post_status' ] ) ) {
$query->set( 'post_status', $_GET[ 'post_status' ] );
}
$filter_key = $_GET[ 'reply_filter' ];
switch ( $filter_key ) {
case 'not_reply':
$meta_query =
array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'et_replies_count',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'et_replies_count',
'value' => 0,
'compare' => '='
)
),
);
$query->set( 'meta_query', $meta_query );
break;
case 'wait_customer_reply':
$mod_ids = et_get_moderator_supporter_id();
$meta_query = array(
'relation' => 'AND',
array(
'relation' => 'AND',
array(
'key' => 'et_last_author',
'value' => $mod_ids,
'compare' => 'IN'
)
)
);
$query->set( 'meta_query', $meta_query );
$query->set( 'author__not_in', $mod_ids );
break;
case 'wait_mod_reply':
$mod_ids = et_get_moderator_supporter_id();
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'et_last_author',
'value' => $mod_ids,
'compare' => 'NOT IN'
),
array(
'key' => 'et_last_author',
'compare' => 'NOT EXISTS'
),
);
$query->set( 'meta_query', $meta_query );
$query->set( 'author__not_in', $mod_ids );
break;
}
}
return $query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment