Add a special filter link to All Posts in admin

All we need is an easy explanation of the problem, so here it is.

I have a featured posts slider on my site’s front page. It displays n posts (5 right now) from the category featured. If I have 5 posts in that category, and publish a 6th, the oldest of the 6 doesn’t show up in the slider or below in the list of all recent posts. My solution right now is to go to All Posts > Published > Featured, pick one to un-feature, and go on with my life.

I’ll take suggestions on a better solution (fix the featured slider is probably a decent response), but what I’m looking for here is a way to add a link to All Posts that will show me all published featured posts.

enter image description here

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

First of all in your image you can see that WordPress lets you filter the posts by category, just bellow the what you have added to the image, But if you want to know how you can add your own link to the filter you can use the views_edit-post filter hook ex:

add_action('pre_get_posts', 'query_add_filter' );
function query_add_filter( $wp_query ) {
    if( is_admin()) {
        add_filter('views_edit-post', 'Add_My_filter');
    }
}

// add filter
function Add_My_filter($views) {
    global $wp_query;
    unset($views['mine']);
    $my_cat = YOUR-CAT-ID

    $query = array(
        'author'      => $current_user->ID,
        'post_type'   => 'post',
        'post_status' => 'publish',
    'cat'         => $my_cat
    );
    $result = new WP_Query($query);
    $class = ($wp_query->query_vars['cat'] == 'featured') ? ' class="current"' : '';
    $views['publish_f'] = sprintf(__('<a href="%s" rel="nofollow noreferrer noopener"'. $class .'>Publish Featured <span class="count">(%d)</span></a>', 'publish featured'),
        admin_url('edit.php?post_status=publish&post_type=post&cat='.$my_cat),
        $result->found_posts);

    return $views;
}

just make sure you change YOUR-CAT-ID to the actual category id

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply