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:
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:
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?
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. I have used in a recent project and it works great.
Source: http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.