Skip to content

Instantly share code, notes, and snippets.

@marushu
Last active November 21, 2023 05:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save marushu/426626851afaadb1e3972de49c34b0b9 to your computer and use it in GitHub Desktop.
Save marushu/426626851afaadb1e3972de49c34b0b9 to your computer and use it in GitHub Desktop.
Supports multiple filters.
<?php
function multiple_filter_at_cpt() {
global $typenow;
$args = array(
'public' => true,
'_builtin' => true,
);
$custom_post_types = get_post_types( $args );
$custom_post_types = array_values( $custom_post_types );
if( empty( $typenow ) || in_array( $typenow, $custom_post_types ) === true )
return;
$taxonomies = get_object_taxonomies( $typenow );
$post_type = get_post_type();
if ( $typenow == $post_type ) {
foreach ( $taxonomies as $tax ) {
if ( ! empty( $tax ) && ! is_wp_error( $tax ) ) {
$selected = isset( $_GET[ $tax ] ) ? $_GET[ $tax ] : '';
$info_taxonomy = get_taxonomy( $tax );
wp_dropdown_categories( array(
'show_option_all' => __( "Show All {$info_taxonomy->label}" ),
'taxonomy' => $tax,
'name' => $tax,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
) );
}
}
}
}
add_action( 'restrict_manage_posts', 'multiple_filter_at_cpt' );
/**
* Set multiple filter at custom post type. And fire.
*
* @param $query
*/
function multiple_filter_taxonomy_term( $query ) {
global $pagenow, $typenow;
$taxonomies = get_object_taxonomies( $typenow );
$post_type = $query->query['post_type'];
if ( ! empty( $taxonomies ) && ! is_wp_error( $taxonomies ) ) {
foreach ( $taxonomies as $tax ) {
if ( ! empty( $tax ) ) {
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php'
&& isset( $q_vars[ 'post_type' ] )
&& $q_vars[ 'post_type' ] == $post_type
&& isset( $q_vars[ $tax ] )
&& is_numeric( $q_vars[ $tax ] )
&& $q_vars[ $tax ] != 0
) {
$term = get_term_by( 'id', $q_vars[ $tax ], $tax );
$q_vars[ $tax ] = $term->slug;
}
}
}
}
}
add_filter( 'parse_query', 'multiple_filter_taxonomy_term' );
@jon-heller
Copy link

Perfect, thank you!

@ecosyse
Copy link

ecosyse commented Sep 1, 2017

great, thanks!

@murdokland
Copy link

murdokland commented Jul 14, 2021

Hi,
the link after sorting is like edit.php?s&post_status=all&post_type=equipe etc.. and display empty results.
If i remove the s& manually it's works fine.
any idea how to correct that?
thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment