如何在自定义帖子类型的自定义元框的WordPress中添加过滤器?

时间:2016-07-22 06:13:47

标签: php wordpress

我想在图片下面添加过滤器链接 enter image description here

提前谢谢。

2 个答案:

答案 0 :(得分:1)

检查这个

add_action( 'restrict_manage_posts', 'our_drink_filter' );
/**
 * First create the dropdown
 * make sure to change POST_TYPE to the name of your custom post type
 * 
 * @author Ohad Raz
 * 
 * @return void
 */
function our_drink_filter(){
    $type = 'post';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }

    //only add filter to post type you want
    if ('our_drink' == $type){
        //change this to the list of values you want to show
        //in 'label' => 'value' format
        $filter_post = array();
        $all_movies1 = get_posts( array(
            'post_type' => 'our_restaurant',
            'numberposts' => -1,
            'orderby' => 'post_title',
            'order' => 'ASC'
            ) );
            foreach ( $all_movies1 as $movie1 ) :
                //echo $movie1->post_title." ".sanitize_title($movie1->post_title)."<br>";
                array_push($filter_post[$movie1->post_title] = sanitize_title($movie1->post_title));

            endforeach; 
        ?>
        <select name="ADMIN_FILTER_FIELD_VALUE">
        <option value=""><?php _e('Store Name ', 'wose45436'); ?></option>
        <?php
            $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
            foreach ($filter_post as $label => $value) {
                printf
                    (
                        '<option value="%s"%s>%s</option>',
                        $value,
                        $value == $current_v? ' selected="selected"':'',
                        $label
                    );
                }
        ?>
        </select>
        <?php
    }
}


add_filter( 'parse_query', 'drink_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 drink_posts_filter( $query ){
    global $pagenow;
    $type = 'post';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }
    if ( 'our_drink' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
        $query->query_vars['meta_key'] = 'custom_element_grid_class_meta_box';
        $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
    }
}

答案 1 :(得分:-1)

看起来这是一个插件。为此,您需要更改该插件的代码。如果不知道该插件的内容,我就无法开始告诉你,如果加载了该插件的更新,那么你仍然会遇到opton disasaring的问题。所以你最好的选择是从插件的作者那里请求它。