Utilizzi wordpress ma la visualizzazione dei post in amministrazione è troppo confusa?
Hai talmente tanti post che non riesci a trovare quello che ti interessa per modificarlo?
Ecco come filtrare i post per categoria di wordpress.
Introduzione
Perché è utile filtrare i post per categoria? Potrebbe essere utile nel caso di grandi installazioni di wordpress piene di articoli e piene di categorie. Come facciamo a capire quali e quanti post sono in una determinata categoria? E se avessimo un post type personalizzato? Allora anche qui risulterebbe molto utile filtrare i post per categoria.
Ed ecco che per organizzare meglio l’amministrazione di wordpress, ci viene in aiuto il codice che proponiamo oggi.
Quelle che utilizzeremo oggi sono l’action restrict_manage_posts e il filter parse_query. La prima viene utilizzato per modificare la query di default di wordpress e per creare la dropdown che visualizzeremo nell’elenco dei post e la seconda per interagire con la query. Ok basta parole e passiamo al codice.
Step 1 – aggiungiamo il menu a tendina
add_action( 'restrict_manage_posts', 'taxonomy_filter_restrict_manage_posts' );
/**
* taxonomy filter
* @param
*/
function taxonomy_filter_restrict_manage_posts() {
//build drop down
global $typenow, $wp_query;
$post_types = get_post_types( array( '_builtin' => false ) );
if ( in_array( $typenow, $post_types ) ) {
$filters = get_object_taxonomies( $typenow );
foreach ( $filters as $tax_slug ) {
$tax_obj = get_taxonomy( $tax_slug );
// check if anything has been selected, else set selected to null
$selected = isset($wp_query->query[$tax_slug]) ? $wp_query->query[$tax_slug] : null;
wp_dropdown_categories( array(
'show_option_all' => __('Show All ' . $tax_obj->label . ' '),
'taxonomy' => $tax_slug,
'name' => $tax_obj->name,
'orderby' => 'name',
'selected' => $selected,
'hierarchical' => $tax_obj->hierarchical,
'show_count' => false,
'hide_empty' => false
) );
}
}
}
Step 2 – Interagiamo con la query
add_filter( 'parse_query', 'taxonomy_filter_post_type_request');
/**
* taxonomy filter
* @param
*/
function taxonomy_filter_post_type_request( $query ) {
//add filter to query so dropdown will work
global $pagenow, $typenow;
if ( 'edit.php' != $pagenow )
return;
$filters = get_object_taxonomies( $typenow );
foreach ( $filters as $tax_slug ) {
$var = $query->query_vars[$tax_slug];
if ( $var != 0 ) { // query string has value of 0 if no term is selected
$term = get_term_by( 'id', $var, $tax_slug );
$var = $term->slug;
}
}
Ed ecco che questo codice, che potete incollare nel functions.php del tema, aggiungerà un menu a tendina nell’elenco dei post per poter filtrare gli articoli per categoria.
P.s: Questo codice aggiungerà il menu a tendina anche ai vostri Post Type personalizzati.
OO – Class
Per gli appassionati della programmazione ad oggetti, ecco la stessa implementazione in OOP
/**
* Add post category Filters
*
* @class CategoryFilters
* @author david.silvestri @dot4all.it
*/
class CategoryFilters{
protected static $_instance = null;
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __construct() {
add_action( 'restrict_manage_posts', array( $this, 'taxonomy_filter_restrict_manage_posts') );
add_filter( 'parse_query', array( $this,'taxonomy_filter_post_type_request') );
}
/**
* taxonomy filter
* @param
*/
function taxonomy_filter_restrict_manage_posts() {
//build drop down
global $typenow, $wp_query;
$post_types = get_post_types( array( '_builtin' => false ) );
if ( in_array( $typenow, $post_types ) ) {
$filters = get_object_taxonomies( $typenow );
foreach ( $filters as $tax_slug ) {
$tax_obj = get_taxonomy( $tax_slug );
// check if anything has been selected, else set selected to null
$selected = isset($wp_query->query[$tax_slug]) ? $wp_query->query[$tax_slug] : null;
wp_dropdown_categories( array(
'show_option_all' => __('Show All ' . $tax_obj->label . ' '),
'taxonomy' => $tax_slug,
'name' => $tax_obj->name,
'orderby' => 'name',
'selected' => $selected,
'hierarchical' => $tax_obj->hierarchical,
'show_count' => false,
'hide_empty' => false
) );
}
}
}
/**
* taxonomy filter
* @param
*/
function taxonomy_filter_post_type_request( $query ) {
//add filter to query so dropdown will work
global $pagenow, $typenow;
if ( 'edit.php' != $pagenow )
return;
$filters = get_object_taxonomies( $typenow );
foreach ( $filters as $tax_slug ) {
$var = $query->query_vars[$tax_slug];
if ( $var != 0 ) { // query string has value of 0 if no term is selected
$term = get_term_by( 'id', $var, $tax_slug );
$var = $term->slug;
}
}
}
}
function category_filters() {
return CategoryFilters::instance();
}
add_action( 'admin_init', 'category_filters' );
Dove inserire questa classe?
Potete incollare il codice in un file all’interno del tema attivo ed includerlo poi nel functions.php
Dettagli
Livello: basso
Compatibilità: testato su WP versione 3.9.1





