Created
February 17, 2012 01:25
-
-
Save carlthewebmaster/1849549 to your computer and use it in GitHub Desktop.
Show only author's articles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hide articles from contribs who don't own them | |
function show_author_posts_only($query) { | |
global $user_level; | |
if($query->is_admin && $user_level < 5) { | |
global $user_ID; | |
$query->set('author', $user_ID); | |
unset($user_ID); | |
} | |
unset($user_level); | |
return $query; | |
} | |
add_filter('pre_get_posts', 'show_author_posts_only'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Show only posts related to current user | |
add_action('pre_get_posts', 'query_set_only_author' ); | |
function query_set_only_author( $wp_query ) { | |
global $current_user; | |
if( is_admin() && !current_user_can('edit_others_posts') ) { | |
$wp_query->set( 'author', $current_user->ID ); | |
add_filter('views_edit-post', 'fix_post_counts'); | |
} | |
} | |
// Fix post counts | |
function fix_post_counts($views) { | |
global $current_user, $wp_query; | |
unset($views['mine']); | |
$types = array( | |
array( 'status' => NULL ), | |
array( 'status' => 'publish' ), | |
array( 'status' => 'draft' ), | |
array( 'status' => 'pending' ), | |
array( 'status' => 'trash' ) | |
); | |
foreach( $types as $type ) { | |
$query = array( | |
'author' => $current_user->ID, | |
'post_type' => 'post', | |
'post_status' => $type['status'] | |
); | |
$result = new WP_Query($query); | |
if( $type['status'] == NULL ): | |
$class = ($wp_query->query_vars['post_status'] == NULL) ? ' class="current"' : ''; | |
$views['all'] = sprintf(__('<a href="%s"'. $class .'>All <span class="count">(%d)</span></a>', 'all'), | |
admin_url('edit.php?post_type=post'), | |
$result->found_posts); | |
elseif( $type['status'] == 'publish' ): | |
$class = ($wp_query->query_vars['post_status'] == 'publish') ? ' class="current"' : ''; | |
$views['publish'] = sprintf(__('<a href="%s"'. $class .'>Published <span class="count">(%d)</span></a>', 'publish'), | |
admin_url('edit.php?post_status=publish&post_type=post'), | |
$result->found_posts); | |
elseif( $type['status'] == 'draft' ): | |
$class = ($wp_query->query_vars['post_status'] == 'draft') ? ' class="current"' : ''; | |
$views['draft'] = sprintf(__('<a href="%s"'. $class .'>Draft'. ((sizeof($result->posts) > 1) ? "s" : "") .' <span class="count">(%d)</span></a>', 'draft'), | |
admin_url('edit.php?post_status=draft&post_type=post'), | |
$result->found_posts); | |
elseif( $type['status'] == 'pending' ): | |
$class = ($wp_query->query_vars['post_status'] == 'pending') ? ' class="current"' : ''; | |
$views['pending'] = sprintf(__('<a href="%s"'. $class .'>Pending <span class="count">(%d)</span></a>', 'pending'), | |
admin_url('edit.php?post_status=pending&post_type=post'), | |
$result->found_posts); | |
elseif( $type['status'] == 'trash' ): | |
$class = ($wp_query->query_vars['post_status'] == 'trash') ? ' class="current"' : ''; | |
$views['trash'] = sprintf(__('<a href="%s"'. $class .'>Trash <span class="count">(%d)</span></a>', 'trash'), | |
admin_url('edit.php?post_status=trash&post_type=post'), | |
$result->found_posts); | |
endif; | |
} | |
return $views; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Show only posts related to current user | |
add_action('pre_get_posts', 'query_set_only_author' ); | |
function query_set_only_author( $wp_query ) { | |
global $current_user; | |
if( !current_user_can('editor') && !current_user_can('administrator') ) { | |
$wp_query->set( 'author', $current_user->ID ); | |
add_filter('views_edit-post', 'fix_post_counts'); | |
} | |
} | |
// Fix post counts | |
function fix_post_counts($views) { | |
global $current_user, $wp_query; | |
unset($views['mine']); | |
$types = array( | |
array( 'status' => NULL ), | |
array( 'status' => 'publish' ), | |
array( 'status' => 'draft' ), | |
array( 'status' => 'pending' ), | |
array( 'status' => 'trash' ) | |
); | |
foreach( $types as $type ) { | |
$query = array( | |
'author' => $current_user->ID, | |
'post_type' => 'post', | |
'post_status' => $type['status'] | |
); | |
$result = new WP_Query($query); | |
if( $type['status'] == NULL ): | |
$class = ($wp_query->query_vars['post_status'] == NULL) ? ' class="current"' : ''; | |
$views['all'] = sprintf(__('<a href="%s"'. $class .'>All <span class="count">(%d)</span></a>', 'all'), | |
admin_url('edit.php?post_type=post'), | |
$result->found_posts); | |
elseif( $type['status'] == 'publish' ): | |
$class = ($wp_query->query_vars['post_status'] == 'publish') ? ' class="current"' : ''; | |
$views['publish'] = sprintf(__('<a href="%s"'. $class .'>Published <span class="count">(%d)</span></a>', 'publish'), | |
admin_url('edit.php?post_status=publish&post_type=post'), | |
$result->found_posts); | |
elseif( $type['status'] == 'draft' ): | |
$class = ($wp_query->query_vars['post_status'] == 'draft') ? ' class="current"' : ''; | |
$views['draft'] = sprintf(__('<a href="%s"'. $class .'>Draft'. ((sizeof($result->posts) > 1) ? "s" : "") .' <span class="count">(%d)</span></a>', 'draft'), | |
admin_url('edit.php?post_status=draft&post_type=post'), | |
$result->found_posts); | |
elseif( $type['status'] == 'pending' ): | |
$class = ($wp_query->query_vars['post_status'] == 'pending') ? ' class="current"' : ''; | |
$views['pending'] = sprintf(__('<a href="%s"'. $class .'>Pending <span class="count">(%d)</span></a>', 'pending'), | |
admin_url('edit.php?post_status=pending&post_type=post'), | |
$result->found_posts); | |
elseif( $type['status'] == 'trash' ): | |
$class = ($wp_query->query_vars['post_status'] == 'trash') ? ' class="current"' : ''; | |
$views['trash'] = sprintf(__('<a href="%s"'. $class .'>Trash <span class="count">(%d)</span></a>', 'trash'), | |
admin_url('edit.php?post_status=trash&post_type=post'), | |
$result->found_posts); | |
endif; | |
} | |
return $views; | |
} | |
//if (current_user_can('contributor') || current_user_can('author')) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment