将过滤器菜单添加到帖子的管理列表(自定义类型),以按自定义字段值过滤帖子


45

这个答案与我要执行的操作非常接近,但是我想指定一个特定的自定义字段并显示其可用值的选择菜单。谢谢!


1
@soulseekah在尝试玩东西之前先问路不是合法的吗?
frnhr 2013年

Answers:


73

简单易行,首先仅使用所需的元值创建下拉列表,然后捕获过滤器的提交,只需更改POST_TYPE为帖子类型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'];
    }
}

优秀的!我无法将其显示在我的监护人类型的帖子页面上(类型=竞赛)。您能检查一下我的functions.php代码吗?这是从现有插件中获取的吗? pastebin.com/BJMua8nq
adam5280 2012年

您没有更改POST_TYPE此处的位置,请尝试以下操作:pastebin.com/tabUfh3Y
Bainternet 2012年

作品!谢谢@Bainternet!$ type ='post'; 在第65行也必须更改。再次感谢!
adam5280

:)您不需要更改的唯一一个,它是默认设置,但是也可以。
Bainternet 2012年

2
此答案适用于WordPress 4.9.5!它的年龄很好,谢谢!
戴维

2

如果在内部使用其他查询,请restrict_manage_posts确保将其添加&& $query->is_main_query()到parse_query if语句中,否则parse_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'];
}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.