Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom drop down filter in wordpress WP_List_Table

Tags:

wordpress

I extended class WP_List_Table to display listing records of Custom Database Table. Listing is successful but I'm getting mad about how to implement a drop-down filter to filter my custom db table records according to its categories.

Please share any code to add drop-down filter to filter my custom database table records. Field name is cat_id.

like image 785
Mian Majid Avatar asked May 25 '14 20:05

Mian Majid


People also ask

How do I create a custom filter in WordPress?

To install Woo Custom Filter Widget, log in to your WordPress dashboard, navigate to the Plugins menu, and click “Add New.” In the search field type “Woo Custom Filter Widget,” then click “Search Plugins.” Once you've found us, Click “Install Now,” and WordPress will take it from there.


1 Answers

after struggle of 3-hours after posting question here, i explored the class, and found the solution, so i am sharing information here.

There is a function function extra_tablenav( $which ), I override that function with my function,

function extra_tablenav( $which ) {
    global $wpdb, $testiURL, $tablename, $tablet;
    $move_on_url = '&cat-filter=';
    if ( $which == "top" ){
        ?>
        <div class="alignleft actions bulkactions">
        <?php
        $cats = $wpdb->get_results('select * from '.$tablename.' order by title asc', ARRAY_A);
        if( $cats ){
            ?>
            <select name="cat-filter" class="ewc-filter-cat">
                <option value="">Filter by Category</option>
                <?php
                foreach( $cats as $cat ){
                    $selected = '';
                    if( $_GET['cat-filter'] == $cat['id'] ){
                        $selected = ' selected = "selected"';   
                    }
                    $has_testis = false;
                    $chk_testis = $wpdb->get_row("select id from ".$tablet." where banner_id=".$cat['id'], ARRAY_A);
                    if( $chk_testis['id'] > 0 ){
                ?>
                <option value="<?php echo $move_on_url . $cat['id']; ?>" <?php echo $selected; ?>><?php echo $cat['title']; ?></option>
                <?php   
                    }
                }
                ?>
            </select>
            <?php   
        }
        ?>  
        </div>
        <?php
    }
    if ( $which == "bottom" ){
        //The code that goes after the table is there

    }
}

and then I jumped into function prepare_items() and added a line after query string,

if( $_GET['cat-filter'] > 0 ){
            $query = $query . ' where cat_id=' . $_GET['cat-filter'];   
        }

not finished here, I added some lines of javascript to execute drop down,

$('.ewc-filter-cat').live('change', function(){
    var catFilter = $(this).val();
    if( catFilter != '' ){
        document.location.href = 'admin.php?page=ewc-testimonial'+catFilter;    
    }
});

and its working cool and fine, if anybody need more help then comment here.

Thank you for time.

like image 71
Mian Majid Avatar answered Oct 06 '22 07:10

Mian Majid