Skip to content

Instantly share code, notes, and snippets.

@cadic
Created November 5, 2015 15:22
Show Gist options
  • Save cadic/8224ebc8831080bf9573 to your computer and use it in GitHub Desktop.
Save cadic/8224ebc8831080bf9573 to your computer and use it in GitHub Desktop.
Wordpress plugin filter posts by author
<?php
/*
Plugin Name: Author Filter
Description: Adds author dropdown to post list filters
Author: Max Lyuchin
Author URI: http://heartwp.com/
*/
add_action( 'restrict_manage_posts', 'ml_author_dropdown' );
function ml_author_dropdown()
{
$args = array(
'show_option_all' => __( 'All authors' ),
'name' => 'author',
);
if ( isset( $_GET['author'] ) ) {
$args[ 'selected' ] = $_GET[ 'author' ];
}
wp_dropdown_users( $args );
}
add_filter( 'parse_query', 'ml_filter_posts_by_author' );
function ml_filter_posts_by_author( $query )
{
if ( is_admin() && isset( $_GET[ 'author' ] ) && $_GET[ 'author' ] != 0 ) {
$query->query_vars[ 'author' ] = $_GET[ 'author' ];
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment