답변:
간단하게 수행하려면 먼저 원하는 메타 값만으로 드롭 다운을 만든 다음 필터 제출을 확인하고 POST_TYPE
게시물 유형 META_KEY
이름과 메타 키 이름으로 변경 하면 됩니다.
<?php
/*
Plugin Name: Admin Filter BY Custom Fields
Plugin URI: http://en.bainternet.info
Description: answer to http://wordpress.stackexchange.com/q/45436/2487
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/
add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );
/**
* First create the dropdown
* make sure to change POST_TYPE to the name of your custom post type
*
* @author Ohad Raz
*
* @return void
*/
function wpse45436_admin_posts_filter_restrict_manage_posts(){
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
//only add filter to post type you want
if ('POST_TYPE' == $type){
//change this to the list of values you want to show
//in 'label' => 'value' format
$values = array(
'label' => 'value',
'label1' => 'value1',
'label2' => 'value2',
);
?>
<select name="ADMIN_FILTER_FIELD_VALUE">
<option value=""><?php _e('Filter By ', 'wose45436'); ?></option>
<?php
$current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
foreach ($values as $label => $value) {
printf
(
'<option value="%s"%s>%s</option>',
$value,
$value == $current_v? ' selected="selected"':'',
$label
);
}
?>
</select>
<?php
}
}
add_filter( 'parse_query', 'wpse45436_posts_filter' );
/**
* if submitted filter by post meta
*
* make sure to change META_KEY to the actual meta key
* and POST_TYPE to the name of your custom post type
* @author Ohad Raz
* @param (wp_query object) $query
*
* @return Void
*/
function wpse45436_posts_filter( $query ){
global $pagenow;
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ( 'POST_TYPE' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
$query->query_vars['meta_key'] = 'META_KEY';
$query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
}
}
POST_TYPE
:이 시도 여기 pastebin.com/tabUfh3Y
내부에서 다른 쿼리를 사용하는 경우 parse_query if 문에 restrict_manage_posts
추가 && $query->is_main_query()
해야합니다. 그렇지 않으면 parse_query 필터가 해당 두 번째 쿼리를 방해합니다.
if ( 'POST_TYPE' == $type
&& is_admin()
&& $pagenow=='edit.php'
&& isset($_GET['ADMIN_FILTER_FIELD_VALUE'])
&& $_GET['ADMIN_FILTER_FIELD_VALUE'] != ''
&& $query->is_main_query()
) {
$query->query_vars['meta_key'] = 'META_KEY';
$query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
}