简体   繁体   English

Wordpress 管理员更改查询

[英]Wordpress Admin change query

I'm trying to filter the 'All posts' admin screen in order to retrieve posts with unique categories.我正在尝试过滤“所有帖子”管理屏幕,以便检索具有唯一类别的帖子。 By now, I've manage to set a $_GET key in the url with prase_query hook:到现在为止,我已经设法使用 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'];
    }
}

Later on, using restrict_manage_posts hook, I've created a dropdown containing all my post's category:后来,使用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 }

It is possible using this 'category_only' key to alter the loop in order to retrieve post with only one category?是否可以使用这个“category_only”键来改变循环以检索只有一个类别的帖子?

Thank you谢谢

I suggest using this code:我建议使用此代码:

/**
 * 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;
    }
}

You just need to change CPT and Taxonomy.您只需要更改 CPT 和分类法。 I have used in a recent project and it works great.我在最近的一个项目中使用过,效果很好。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM