[wp-trac] [WordPress Trac] #23422: Add query filter argument to register_taxonomy
WordPress Trac
noreply at wordpress.org
Fri May 6 11:21:21 UTC 2016
#23422: Add query filter argument to register_taxonomy
-------------------------------------------+------------------------------
Reporter: tifosi | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Taxonomy | Version:
Severity: normal | Resolution:
Keywords: needs-patch reporter-feedback | Focuses: administration
-------------------------------------------+------------------------------
Comment (by tifosi):
Thanks for the reply. Wondered if this would get looks at in the taxonomy
bug scrub.
WP 3.5 added the 'show_admin_column' to allow automatic creation of
taxonomy columns on associated post types.
It is possible to add a select dropdown filter for the taxonomy in such a
situation using judicious use of the 'restrict_manage_posts' action and
'parse_query' filters. This displays between the apply & filter buttons to
the right of the 'show all dates' select.
Would be a functional addition to have a 'show_filter' argument to
register_taxonomy() args to display this filter automatically for
associated post-types. This would only trigger if 'show_admin_column' is
also set to be true/1.
Example code below I've used in my own framework:
{{{#!php
<?php
add_action( 'restrict_manage_posts', '_post_type_filter' );
function _post_type_filter() {
//required presets
global $typenow, $wp_query;
//only if current post type
if ( $typenow !== $this->post_type_name ) { return; }
//get current taxonomy
$current_taxonomy = get_taxonomy( $this->taxonomy_name );
//only if query_var
if ( empty( $current_taxonomy->query_var ) ) { return; }
//terms
$tax_terms = get_terms( $this->taxonomy_name );
$tax_term_count = (int)sizeof( $tax_terms );
//need terms...
if ( $tax_term_count === 0 ) { return; }
//dropdown select
wp_dropdown_categories(
array(
'show_option_all' => __( "Show All
{$current_taxonomy->label}" ),
'taxonomy' => $this->taxonomy_name,
'name' => $current_taxonomy->name,
'orderby' => 'name',
'selected' => ( isset(
$wp_query->query[$this->taxonomy_name] ) ) ?
$wp_query->query[$this->taxonomy_name] : '',
'hierarchical' => true,
'depth' => 3,
'show_count' => true,
'hide_empty' => true,
)
);
}
}}}
{{{#!php
<?php
add_filter( 'parse_query', array( $this, '_post_type_filter_query') );
public function _post_type_filter_query( $query ) {
//required page
global $pagenow;
//test page
if ( $pagenow !== 'edit.php' ) { return; }
//set filter
$vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset( $vars[ $this->taxonomy_name ] )
&& is_numeric( $vars[$this->taxonomy_name] ) ) {
$term = get_term_by( 'id', $vars[ $this->taxonomy_name ],
$this->taxonomy_name );
if ( $term ) { $vars[ $this->taxonomy_name ] = $term->slug; }
}
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/23422#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list