2

I'm trying to filter my post in my admin area with a custom filter dropdown.

I use this method to make it: https://wordpress.stackexchange.com/a/45447/84541

Here is my code:

   add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );

    function wpse45436_admin_posts_filter_restrict_manage_posts(){
        $type = 'post';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }

        //only add filter to post type you want

        if ('post' == $type){
            //change this to the list of values you want to show
            //in 'label' => 'value' format
            $values = array();
            $testting = get_posts(array('posts_per_page'=>-1, 'post_type'=>'edition'));
            foreach($testting as $post):
                $values[$post->post_title] = $post->ID;


            endforeach; 


            ?>

            <select name="ADMIN_FILTER_FIELD_VALUE">
            <option value=""><?php _e('Toutes les éditions ', 'wose45436'); ?></option>
            <?php
                $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
                foreach ($values as $label => $value) {
                    printf(
                            '<option value="%s"%s>%s</option>',
                            $value,
                            $value == $current_v? ' selected="selected"':'',
                            $label
                        );
                    }
            ?>
            </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 ( 'post' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
            $query->query_vars['meta_key'] = 'n_de_ldition';
            $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
        }
    }

When I click on filter after selecting my option in my dropdown select, It does filter fine but my query who was adding option in my select is empty. I can't understand what I'm doing wrong, is this because I use a get_posts to get my custom post type informations?

Here are some image. When not filtered but clicked on select dropdown: Before filtering

After filtering, options from selected dropdown are no more avaible but filters has worked fine: After Filtering

If I replace this:

            $values = array();
            $testting = get_posts(array('posts_per_page'=>-1, 'post_type'=>'edition'));
            foreach($testting as $post):
                $values["N°".$post->post_title] = $post->ID;


            endforeach; 

whit this:

    $values = array(
        'label' => 'value', 
        'label1' => 'value1',
        'label2' => 'value2',
    );

It works fine but I have to change my labels and my values dynamicly.

Any Idea?

Thank's in advance.

4
  • You can try to add $query->is_main_query() in the check inside the wpse45436_posts_filter() function.
    – birgire
    Dec 1, 2015 at 15:54
  • Glad to hear it worked for you - You can post this as an answer and accept it to close the question
    – birgire
    Dec 2, 2015 at 9:26
  • Ok, I can accept tomorrow according to rules of Stackexchange.
    – Ceyar
    Dec 2, 2015 at 9:37
  • Great, I adjusted your answer - adjusted the formatting and added some more explanations.
    – birgire
    Dec 2, 2015 at 9:47

1 Answer 1

4

Thank you so much, it's working. Here is my modified code:

function wpse45436_posts_filter( $query ){
    global $pagenow;
    $type = 'post';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }
    if ( 
        'post' == $type  
         && is_admin()  
         && $pagenow =='edit.php'  
         && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) 
         && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '' 
         && $query->is_main_query()
     ) {
            $query->query_vars['meta_key'] = 'n_de_ldition';
            $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
        }
    }

By targetting the main query with $query->is_main_query() we avoid modifying our get_posts() call that we use to populate the select box. The reason is that get_posts() is just a wrapper for the WP_Query so the parse_query hook can affect it as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.