jacodelucia
4/12/2016 - 2:35 PM

WP - FILTRES ADMIN

WP - FILTRES ADMIN

<?php


/************************************************************************************************/
/* FILTRES D'AFFICHAGE DE LA GALERIE PAR LETTRE DANS L'ADMIN  */
/************************************************************************************************/


add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );

function wpse45436_admin_posts_filter_restrict_manage_posts(){
    global $letters;
    $type = 'post';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }
	
    if ('galerie' == $type){
        ?>
        <select name="GALLERIE_FILTER">
        <option value="">Toutes les lettres </option>
        <?php
            $current_v = isset($_GET['GALLERIE_FILTER'])? $_GET['GALLERIE_FILTER']:'';
            foreach ($letters as $letter) {
                printf
                    (
                        '<option value="%s"%s>%s</option>',
                        $letter['letter'],
                        $letter['letter'] == $current_v? ' selected="selected"':'',
                        strtoupper($letter['letter'])
                    );
                }
        ?>
        </select>
        <?php
    }
}


add_filter( 'parse_query', 'wpse45436_posts_filter' );
function wpse45436_posts_filter( $query ){
    global $pagenow;
    $type = 'post';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }
    if ( 'galerie' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['GALLERIE_FILTER']) && $_GET['GALLERIE_FILTER'] != '') {
        $query->query_vars['meta_key'] = 'lettre';
        $query->query_vars['meta_value'] = $_GET['GALLERIE_FILTER'];
    }
}