restrict_manage_posts의 동작은 트리거 add_extra_tablenav()
당신이 원하는 목록 표에 추가 드롭 다운을 추가하는 방법 인 기능을.
아래 예에서는 먼저 게시물 유형 이 올바른지 확인한 다음 테이블 의 competition_name
키에 대해 저장된 모든 DB 값을 가져 옵니다 postmeta
(필요에 따라 키 이름을 변경해야 함). 쿼리는 상당히 기본적이며 경쟁 이 게시 되었는지 확인 하고 드롭 다운에서 중복을 원하지 않는 고유 한 값만 취한 다음 알파벳순으로 정렬합니다.
다음으로 결과를 확인하고 (아무것도 드롭 다운을 출력하지 않음) 옵션 (모두 표시하는 defualt 포함)을 구성하십시오. 마지막으로 드롭 다운이 출력됩니다.
내 의견에서 언급했듯이, 이것은 이야기의 끝이 아닙니다. 필터가 활성화 된 경우 원하는 결과 만 표시하도록 목록 테이블에 지시하는 논리가 필요하지만 추가 지원이 필요한 경우이를 살펴보고 다른 질문을 시작하겠습니다. 힌트 -파일을 확인하십시오. /wp-admin/includes/class-wp-posts-list-table.php
부모입니다..../wp-class-list-table.php
/**
* Add extra dropdowns to the List Tables
*
* @param required string $post_type The Post Type that is being displayed
*/
add_action('restrict_manage_posts', 'add_extra_tablenav');
function add_extra_tablenav($post_type){
global $wpdb;
/** Ensure this is the correct Post Type*/
if($post_type !== 'competition')
return;
/** Grab the results from the DB */
$query = $wpdb->prepare('
SELECT DISTINCT pm.meta_value FROM %1$s pm
LEFT JOIN %2$s p ON p.ID = pm.post_id
WHERE pm.meta_key = "%3$s"
AND p.post_status = "%4$s"
AND p.post_type = "%5$s"
ORDER BY "%3$s"',
$wpdb->postmeta,
$wpdb->posts,
'competition_name', // Your meta key - change as required
'publish', // Post status - change as required
$post_type
);
$results = $wpdb->get_col($query);
/** Ensure there are options to show */
if(empty($results))
return;
// get selected option if there is one selected
if (isset( $_GET['competition-name'] ) && $_GET['competition-name'] != '') {
$selectedName = $_GET['competition-name'];
} else {
$selectedName = -1;
}
/** Grab all of the options that should be shown */
$options[] = sprintf('<option value="-1">%1$s</option>', __('All Competitions', 'your-text-domain'));
foreach($results as $result) :
if ($result == $selectedName) {
$options[] = sprintf('<option value="%1$s" selected>%2$s</option>', esc_attr($result), $result);
} else {
$options[] = sprintf('<option value="%1$s">%2$s</option>', esc_attr($result), $result);
}
endforeach;
/** Output the dropdown menu */
echo '<select class="" id="competition-name" name="competition-name">';
echo join("\n", $options);
echo '</select>';
}
restrict_manage_posts
동작 후크를 사용하여 드롭 다운 상자를 추가 할 수 있습니다 . WP는 자동으로 처리 할 수있는 분류 체계 드롭 다운 목록과 달리 WP가 상자에서 처리 할 내용을 알지 못하므로 필터에 대한 일부 논리를 추가해야한다는 것을 잊지 마십시오.