//Filter articles by custom fields add_filter( 'parse_query', 'zm_posts_filter' ); add_action( 'restrict_manage_posts', 'zm_filter_field' ); function zm_posts_filter( $query ) { global $pagenow; if ( is_admin() && $pagenow=='edit.php' && isset( $_GET['ZMINGCX_FILTER_FIELD']) && $_GET['ZMINGCX_FILTER_FIELD'] != '' ) { $query->query_vars['meta_key'] = $_GET['ZMINGCX_FILTER_FIELD']; if ( isset($_GET['ZMINGCX_FIELD_VALUE'] ) && $_GET['ZMINGCX_FIELD_VALUE'] != '' ) $query->query_vars['meta_value'] = $_GET['ZMINGCX_FIELD_VALUE']; } } function zm_filter_field() { global $wpdb; $sql = 'SELECT DISTINCT meta_key FROM '.$ wpdb->postmeta.' ORDER BY 1'; $fields = $wpdb->get_results( $sql, ARRAY_N ); ?> <select name="ZMINGCX_FILTER_FIELD"> <option value=""><? Php _e ('Custom Field ',' zm ');?></ option> <? php $current = isset( $_GET['ZMINGCX_FILTER_FIELD'] )? $_ GET['ZMINGCX_FILTER_FIELD']:''; $current_v = isset( $_GET['ZMINGCX_FIELD_VALUE'] )? $_ GET['ZMINGCX_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 ',' zm ');?>< input type="TEXT" name="ZMINGCX_FIELD_VALUE" value="<? php echo $current_v; ?>" /> <? php }