The restrict_manage_posts action triggers the add_extra_tablenav()
function, which is how you add additional dropdowns to your desired List Table.
In the example below, we first ensure that the Post Type is correct, and then we grab all DB values stored against the competition_name
key in the postmeta
table (you must change the key name as required). The query is fairly basic and only checks to see if the Competition is published, takes only unique values (you don't want duplication in the dropdown) and then orders them alphabetically.
Next we check for results (no point outputting the dropdown for nothing), and then construct the options (including a defualt to show all). Finally the dropdown is output.
As stated in my comment, this isn't the end of the story though; you'll need some logic to tell the List Table to only show your desired results when the filter is active, but I'll leave you to have a look at that and then start another question if you require further assistance. Hint - check out the file /wp-admin/includes/class-wp-posts-list-table.php
, and it's parent .../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 "%6$s"',
$wpdb->postmeta,
$wpdb->posts,
'competition_name', // Your meta key - change as required
'publish', // Post status - change as required
$post_type,
'competition_name'
);
$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
action hook to add additional dropdown boxes. Don't forget that you'd also have to add some logic for the filter though, as WP won't know what to do with it out of the box (unlike taxonomy dropdown lists, which it can automatically handle).