如何使用自定义字段(搜索功能)过滤帖子列表(在WP仪表板帖子列表中)?
1 个回答
- 投票数
-
- 2011-05-07
我为此专门编写了一个插件,却从没发布过:
用法:
在下拉列表中,您可以找到所有自定义字段的列表,因此只需选择要作为过滤依据的字段,然后单击过滤.如果要过滤为自定义字段的特定值,请选择该字段的名称,输入所需的值,然后单击过滤.
<?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 }
I coded a plugin just for that and never got around to publish it :
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 }
-
为什么不为您的选择框使用助手功能?即.替换`$field [0]==$ current?'selected=" selected"':'', 与`selected($field [0]==$ current,true,false),`... :)Why not use the helper function for your selection box? ie. replace `$field[0] == $current? ' selected="selected"':'', ` with `selected( $field[0] == $current, true, false ),` ... :)
- 7
- 2011-07-18
- t31os
-
这个答案不再起作用.this answer doesn't work anymore.
- 0
- 2018-07-09
- Kir Mazur
-
有没有办法使其在最新版本的WordPress中工作?Is there a way to make it work in newest versions of WordPress?
- 0
- 2018-11-27
- nakkeru
尽管我在Google上搜索了很多,但我还没有找到一个非常简单的问题的答案:
我有一些带有自定义字段(即Supplier_name)的帖子.我希望能够根据此自定义字段搜索和过滤我的帖子.换句话说,在管理员帖子列表中,我想要一个搜索框(名为"供应商名称"),可以在其中键入一个值(例如" IBM"),然后单击一个搜索按钮,我返回所有具有名为" supplier_name"的定制字段的帖子,如果是,则定制字段的值为" IBM".
我该怎么做?