Skip to content

Instantly share code, notes, and snippets.

@gonzalesc
Created December 20, 2024 18:38
Show Gist options
  • Save gonzalesc/9e897b8a801496992dcfcc3b170bbca2 to your computer and use it in GitHub Desktop.
Save gonzalesc/9e897b8a801496992dcfcc3b170bbca2 to your computer and use it in GitHub Desktop.
Tie down authors to see only their own posts on WordPress
<?php
/**
* Tie down authors to see only their own posts
* @package LetsGodev\MuPlugins
* @since 1.0.0
*/
add_filter( 'pre_get_posts', 'tieDownAuthors' );
/**
* Tie down authors to see only their own posts
* Role:
* Administrator
* Editor
* Author
* Contributor
* Subscriber
* @param WP_Query $query
* @return WP_Query
*/
function tieDownAuthors( WP_Query $query ): WP_Query {
global $pagenow;
// If the user is on the posts list page
if ( ! $query->is_admin || 'edit.php' !== $pagenow || $query->get('post_type') !== 'post' ) {
return $query;
}
// If the user can the capability to edit other posts
if ( current_user_can( 'edit_others_posts' ) ) {
return $query;
}
$query->set( 'author', get_current_user_id() );
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment