This answer is very close to what I am looking to do, but instead I would like to specify a specific custom field and display a select menu of its available values. Thanks!
3 Answers
Simple to do, first create the dropdown with just the meta values you want and then catch the submit of the filter, just change POST_TYPE
to the name of your post type and META_KEY
to the name of your 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'];
}
}
-
Excellent! Im having trouble getting this to appear on my custtom type post page (type=contestentry). Could you check my functions.php code please? Is this taken from an existing plugin? pastebin.com/BJMua8nq– adam5280Commented Mar 13, 2012 at 15:57
-
You didn't change the
POST_TYPE
here try this: pastebin.com/tabUfh3Y Commented Mar 13, 2012 at 16:02 -
Works! Thanks @Bainternet! The $type = 'post'; on line 65 had to be changed as well. Thanks again!– adam5280Commented Mar 13, 2012 at 16:12
-
:) The only one you did'nt need to change, it's there for default, but that's OK too. Commented Mar 13, 2012 at 16:22
-
3This answer works a treat on WordPress 4.9.5! It's aged very nicely, thank you!– DaveyCommented Apr 11, 2018 at 11:22
If you are using another query inside restrict_manage_posts
make sure you add && $query->is_main_query()
to your parse_query if statement, otherwise the parse_query filter will interfere with that second 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'];
}
If you have to add many fields you have to add to query
$query->query_vars['meta_query'][] = array(
'key' => 'KEY',
'value' => $_GET['FIELD'],
'compare' => 'LIKE'
);