Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mariusvetrici/25bca5f9252c4b05690c to your computer and use it in GitHub Desktop.
Save mariusvetrici/25bca5f9252c4b05690c to your computer and use it in GitHub Desktop.
Need to filter your Custom Post Types (WordPress) by Author in WPAdmin? Here's a handy way to do it by adding a new drop down for filtering
<?php
add_action( 'restrict_manage_posts', 'admin_posts_filter_restrict_manage_posts_by_author' );
/**
* Create the drop down
*
* @return void
*/
function admin_posts_filter_restrict_manage_posts_by_author(){
if (isset($_GET['post_type']) && 'task' == $_GET['post_type']){
wp_dropdown_users( array(
'show_option_all' => __( 'Show All Authors', 'your-site' ),
'name' => 'bcust_id',
'selected' => $_GET['bcust_id']
));
}
}
add_filter( 'parse_query', 'modify_query_to_filter_by_author' );
/**
* Filter by author
* @param (wp_query object) $query
*
* @return Void
*/
function modify_query_to_filter_by_author( $query ){
global $pagenow;
if ( isset($_GET['post_type'])
&& 'task' == $_GET['post_type']
&& is_admin() &&
$pagenow == 'edit.php'
&& isset($_GET['bcust_id'])
&& $_GET['bcust_id'] != '') {
$query->query_vars['author'] = $_GET['bcust_id'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment