41

Despite the fact I googled a lot, I've not found the answer to a very simple question :

I have some posts with a custom field (i.e. supplier_name). I would like to be able to search and filter my posts according to this custom field. In other words, in the admin posts listing, I would like to have a search box (named "Supplier name") where I can type in a value (ex. "IBM") and then, click on a search button that will give me back all the posts that have a custom field named "supplier_name" and, if so, the value of the custom field will be "IBM".

How can I do this ?

1 Answer 1

46

I coded a plugin just for that and never got around to publish it :

enter image description here

Usage:

In the dropdown you have a list of all custom fields, so just select the field you want to filter by and click filter. if you want to filter to a specific value of a custom field then select the field's name , enter the value you want and click filter.

<?php
/*
Plugin Name: Admin Filter BY Custom Fields
Plugin URI: http://en.bainternet.info
Description: Filter posts or pages in admin by custom fields (post meta)
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/



add_filter( 'parse_query', 'ba_admin_posts_filter' );
add_action( 'restrict_manage_posts', 'ba_admin_posts_filter_restrict_manage_posts' );

function ba_admin_posts_filter( $query )
{
    global $pagenow;
    if ( is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_NAME']) && $_GET['ADMIN_FILTER_FIELD_NAME'] != '') {
        $query->query_vars['meta_key'] = $_GET['ADMIN_FILTER_FIELD_NAME'];
    if (isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '')
        $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
    }
}

function ba_admin_posts_filter_restrict_manage_posts()
{
    global $wpdb;
    $sql = 'SELECT DISTINCT meta_key FROM '.$wpdb->postmeta.' ORDER BY 1';
    $fields = $wpdb->get_results($sql, ARRAY_N);
?>
<select name="ADMIN_FILTER_FIELD_NAME">
<option value=""><?php _e('Filter By Custom Fields', 'baapf'); ?></option>
<?php
    $current = isset($_GET['ADMIN_FILTER_FIELD_NAME'])? $_GET['ADMIN_FILTER_FIELD_NAME']:'';
    $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
    foreach ($fields as $field) {
        if (substr($field[0],0,1) != "_"){
        printf
            (
                '<option value="%s"%s>%s</option>',
                $field[0],
                $field[0] == $current? ' selected="selected"':'',
                $field[0]
            );
        }
    }
?>
</select> <?php _e('Value:', 'baapf'); ?><input type="TEXT" name="ADMIN_FILTER_FIELD_VALUE" value="<?php echo $current_v; ?>" />
<?php
}
4
  • 7
    Why not use the helper function for your selection box? ie. replace $field[0] == $current? ' selected="selected"':'', with selected( $field[0] == $current, true, false ), ... :)
    – t31os
    Jul 18, 2011 at 14:44
  • this answer doesn't work anymore.
    – Kir Mazur
    Jul 9, 2018 at 19:48
  • Is there a way to make it work in newest versions of WordPress?
    – nakkeru
    Nov 27, 2018 at 11:30
  • I have a custom table (Table Name: ssl_transaction_cus) in the database which is used for storing donation information. I fetched all of data from this table in 'admin menu page' with a table(With Table tag). I would like to be able to filter those data according to 'Event Name'. I am trying to according to this answer . But, there are code for custom post or post. But, I have no CPT for those data, just custom data. I am trying for long time. But could not success. Please have a look wordpress.stackexchange.com/questions/408599/…
    – Kaizur
    Aug 15, 2022 at 8:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.