【问题标题】:Wordpress Admin change queryWordpress 管理员更改查询
【发布时间】:2017-01-10 13:30:19
【问题描述】:

我正在尝试过滤“所有帖子”管理屏幕,以便检索具有唯一类别的帖子。 到目前为止,我已经设法使用 prase_query 钩子在 url 中设置了一个 $_GET 键:

add_filter( 'parse_query', 'lxa_admin_posts_filter' );
   function lxa_admin_posts_filter( $query ) {
     global $pagenow;
        if ( is_admin() && $pagenow=='edit.php' && isset($_GET['category_only']) && $_GET['category_only'] != '') {
        $query->query_vars['meta_key'] = $_GET['category_only'];
    }
}

稍后,使用restrict_manage_posts 挂钩,我创建了一个包含我所有帖子类别的下拉列表:

function lxa_admin_posts_filter_restrict_manage_posts() {
global $wpdb;

$categories = get_categories( array(
    'taxonomy' => 'category',
    'orderby' => 'name',
    'parent'  => 0,
    'hierarchical' => true,
) );

?>
<select name="category_only">
<option value=""><?php _e('Filter By Category Only', 'baapf'); ?></option>
<?php   foreach ( $categories as $category ) {
    echo  '<option value= "' . $category -> term_id . '" > ' . $category -> name . '</option>';

}

?>
</select>


<?php }

是否可以使用这个 'category_only' 键来更改循环以检索只有一个类别的帖子?

谢谢

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我建议使用此代码:

    /**
     * Display a custom taxonomy dropdown in admin
     * @author Mike Hemberger
     * @link http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/
     */
    add_action('restrict_manage_posts', 'tsm_filter_post_type_by_taxonomy');
    function tsm_filter_post_type_by_taxonomy() {
        global $typenow;
        $post_type = 'lessons_cpt'; // change to your post type
        $taxonomy  = 'chapters'; // change to your taxonomy
        if ($typenow == $post_type) {
            $selected      = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
            $info_taxonomy = get_taxonomy($taxonomy);
            wp_dropdown_categories(array(
                'show_option_all' => __("Show All {$info_taxonomy->label}"),
                'taxonomy'        => $taxonomy,
                'name'            => $taxonomy,
                'orderby'         => 'name',
                'selected'        => $selected,
                'show_count'      => true,
                'hide_empty'      => true,
            ));
        };
    }
    /**
     * Filter posts by taxonomy in admin
     * @author  Mike Hemberger
     * @link http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/
     */
    add_filter('parse_query', 'tsm_convert_id_to_term_in_query_videos');
    function tsm_convert_id_to_term_in_query_videos($query) {
        global $pagenow;
        $post_type = 'lessons_cpt'; // change to your post type
        $taxonomy  = 'chapters'; // change to your taxonomy
        $q_vars    = &$query->query_vars;
        if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
    
        $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
            $q_vars[$taxonomy] = $term->slug;
        }
    }
    

    您只需要更改 CPT 和分类法。 我在最近的一个项目中使用过,效果很好。

    来源: http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/

    【讨论】:

    • 就像一个魅力。必须做一些调整才能使用我现有的代码
    猜你喜欢
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 2019-10-22
    • 1970-01-01
    相关资源
    最近更新 更多