I need to filter the Posts and Pages tables in the Admin panel by certain shortcodes. In my plugin, I have added a column for shortcodes to the Posts and Pages views that works like the “Categories” and “Tags” columns, displaying a listing of shortcodes that are used in each post. Now I want to be able to click on one of the shortcodes in the column and have it filter the list of posts or pages to just the ones that use that shortcode.
In my custom column, I am outputting this: <a href="?shortcode=my-shortcode-1">[my-shortcode-1]</a>, <a href="?shortcode=my-shortcode-2">[my-shortcode-2]</a>, ...
I just need a filter that will make ?shortcode=...
work. Here’s basically what I have in mind, but I need to know what hook I could use to accomplish this:
add_filter( 'manage_posts_row', 'filter_by_shortcode' ); // I made up this filter
function filter_by_shortcode(){
global $post; // Get the current post
if(!empty($_GET['shortcode']){ // Check for ?shortcode=...
// Check if given shortcode is used in the post
if( has_shortcode( $post->post_content, $_GET['shortcode'] )
return $post; // Return post if shortcode is found
// Return nothing if shortcode is not used
}else{
return $post; // Return post if ?shortcode=... argument is not used
}
}
Is there a hook that fires for each row of the show all Posts/Pages table in the Admin panel? Or is there another filter I can tie into for this?
AFAIK that sort of hook does not exists. You have 2 choiches:
'post_class'
filter hook, check if in admin and if the query string contain a shortcode and if so and if the post has not that shortcode addhidden
class that via core admin CSS is set todisplay: none
. In this way the posts without shortcode are retrieved, but hidden.'posts_where'
and add a SQL where clause usingREGEXP
MySQL function in this way post are not retrievedI prefer the second solution, that seems to me more elegant and performant, however, maybe the core
has_shortcode
function is more reliable than a simple SQL regex.Solution 1
Solution 2
Please note that to make both solution work, the shortcode(s) must be registered via
add_shortcode
, so if the shortcode(s) is(are) registered by yourself or by a third party plugin/theme, be sure that it/they is/are registered in admin screens and before the query happen.Use this filter to add a new column (http://codex.wordpress.org/Plugin_API/Filter_Reference/manage_posts_columns), then this filter to add content to it (http://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column) and with get_post($post_id) in it you can search for your shortcode.