I am trying to create links like the ones in the post tab (see image below).

I created my WP_List_table
but I am not sure how to create those links or how to get them to show a different db query.
I know about the extra_tablenav
function in the class but this adds things after the bulk action area, so this doesn’t seem to do what I want.
As mentioned in the comments, if this is an existing table you wish to add/remove links from, see this answer.
If this a custom subclass of WP_List_Table
, e.g.:
class Wpse56883_WP_List_Table extends WP_List_Table{
//Class methods here
}
Then you add these links by overriding the get_views()
method. This should return an array: array ( id => link )
. This should link back to the page, attaching some value to a query variable (which I’ll call customvar
).
When constructing the links, check for the current value for customvar
and conditionally add the class current
to highlight in bold the current view.
So (inside) your class.
function get_views(){
$views = array();
$current = ( !empty($_REQUEST['customvar']) ? $_REQUEST['customvar'] : 'all');
//All link
$class = ($current == 'all' ? ' class="current"' :'');
$all_url = remove_query_arg('customvar');
$views['all'] = "<a href="https://wordpress.stackexchange.com/questions/56883/{$all_url }" {$class} >All</a>";
//Foo link
$foo_url = add_query_arg('customvar','foo');
$class = ($current == 'foo' ? ' class="current"' :'');
$views['foo'] = "<a href="{$foo_url}" {$class} >Foo</a>";
//Bar link
$bar_url = add_query_arg('customvar','bar');
$class = ($current == 'bar' ? ' class="current"' :'');
$views['bar'] = "<a href="{$bar_url}" {$class} >Bar</a>";
return $views;
}
Then in your prepare_items
method you can retrieve the customvar
method and alter your query according to it’s value.
function prepare_items(){
//Retrieve $customvar for use in query to get items.
$customvar = ( isset($_REQUEST['customvar']) ? $_REQUEST['customvar'] : 'all');
}
Note: The links can be used to perform actions. I would store the ‘action’ value in the query variable action
(remember to use nonces!). Then hook onto load-{$hook}
(see Codex), check permissions and nonces, and then perform the action.
If you are going to include ‘action links’, make sure you use nonces – and you should only display the link for users with the necessary capabilities.