Skip to content

Instantly share code, notes, and snippets.

@alisspers
Last active December 15, 2015 12:39
Show Gist options
  • Save alisspers/5261305 to your computer and use it in GitHub Desktop.
Save alisspers/5261305 to your computer and use it in GitHub Desktop.
Visar hur du gör adminlistor i WordPress filtrerbara m.a.p. taxonomier
<?php
add_action( 'parse_query', 'wp_parse_query' );
function wg_parse_query( $query )
{
global $typenow, $pagenow;
if ( ! is_admin() || $typenow !== 'my_post_type' || $pagenow !== 'edit.php' )
{
// Se till att vi ändrar query:n vid rätt tillfälle
return;
}
$taxonomy_id = 'my-taxonomy'; // Slug för aktuell taxonomi
$query_vars = &$query->query_vars;
if ( isset( $_GET[ $taxonomy_id ] ) && $_GET[ $taxonomy_id ] !== '0' )
{
// Hämta ut vald term
$term = get_term_by( 'id', $_GET[ $taxonomy_id ], $taxonomy_id );
if ( ! is_null( $term ) )
{
// Lägg till en query var för vald term
$query_vars[ $taxonomy_id ] = $term->slug;
}
}
return $query;
}
<?php
add_action( 'restrict_manage_posts', 'wg_add_taxonomy_dropdown' ) );
function wg_add_taxonomy_dropdown()
{
global $typenow;
if ( $typenow !== 'my_post_type' )
{
// Vi vill bara lägga till dropdown för rätt posttyp
return;
}
$taxonomy_id = 'my-taxonomy'; // Slug för aktuell taxonomi
$taxonomy = get_taxonomy( $taxonomy_id );
wp_dropdown_categories(
array(
'show_option_all' => sprintf( __( 'Show all %1$s' ), $taxonomy->label ),
'taxonomy' => $taxonomy_id,
'name' => $taxonomy_id,
'orderby' => 'name',
'hierarchical' => true,
'hide_empty' => true,
'selected' => isset( $_GET[ $taxonomy_id ] ) ? $_GET[ $taxonomy_id ] : 0,
'hide_if_empty' => true,
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment