How to add a custom drop-down filter in wordpress WP_List_Table

I extended the WP_List_Table class to display user database table listing entries. The listing is successful, but I'm angry about how to implement a drop-down filter to filter my user db table entries according to its categories.

Please share any code to add a drop-down filter to filter my custom database table entries. The field name is cat_id.

+7
wordpress
source share
1 answer

after struggling 3 hours after posting the question here, I examined the class and found a solution, so I report the information here.

There is a function function extra_tablenav ($ which), I override this 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 the prepare_items () function and added a line after the query line,

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

not finished here, i added some javascript lines to do drop down,

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

and his work is cool and beautiful, if someone needs extra help, then comment here.

Thank you for your time.

+12
source share

All Articles