restrict_manage_postsのアクションをトリガーするadd_extra_tablenav()
、あなたの希望リスト表に追加のドロップダウンを追加する方法である機能を、。
以下の例では、まず投稿タイプが正しいことを確認してcompetition_name
から、postmeta
テーブルのキーに対して格納されているすべてのDB値を取得します(必要に応じてキー名を変更する必要があります)。クエリはかなり基本的なものであり、コンテストが公開されているかどうかを確認するだけであり、一意の値のみを受け取り(ドロップダウンで重複させたくない)、アルファベット順に並べ替えます。
次に、結果を確認し(ドロップダウンを何も出力しないポイントはありません)、オプションを作成します(すべてを表示するデフォルトを含む)。最後にドロップダウンが出力されます。
私のコメントで述べたように、これで話は終わりではありません。フィルターがアクティブな場合に目的の結果のみを表示するようにリストテーブルに指示するためのロジックが必要ですが、それを見て、さらに支援が必要な場合は別の質問を開始するようにします。ヒント -ファイルをチェックアウトし、/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はボックスから何をすべきかを知らないので(フィルターが自動的に処理できる分類法ドロップダウンリストとは異なり)、フィルターのロジックも追加する必要があることを忘れないでください。