Skip to main content
Tweeted twitter.com/#!/StackWordPress/status/441206688490983424
Change title
Link
gmazzap
  • 46.3k
  • 6
  • 95
  • 147

Hook How to filter "All Posts"backend post list showing only posts having a shortcode

Source Link
Matt
  • 23
  • 4

Hook to filter "All Posts" list

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?