Skip to main content
Fixed 'if' statement
Source Link
Max Yudin
  • 6.4k
  • 2
  • 25
  • 36
<?php
add_filter('pre_get_posts', 'my_current_author_posts');

function my_current_author_posts( $query )
{
    global $pagenow;

    // Check if you are on the right Admin page, do nothing if not
    if( 'edit.php' != $pagenow || ! isset( $_GET['post_type'] ) || 'page' != $query->post_type$_GET['post_type'] || ! is_admin() ) {
        return;
    }

    // get current user Object
    $current_user = wp_get_current_user();

    // check user capabilities
    if( ! current_user_can( 'edit_others_pages' ) ) {

        // remove views for pages, see the function below
        add_action( 'views_edit-page', 'my_remove_views' );

        // set WP_Query 'Author' parameter
        $query->set( 'author', $current_user->ID );
    }

    return $query;
}

// function to remove views
function my_remove_views( $views )
{

    $views_to_remove = array(
        'all',
        'publish',
        'future',
        'sticky',
        'draft',
        'pending',
        'trash',
    );

    foreach ( $views_to_remove as $view_to_remove ) {
        unset( $views[$view_to_remove] );
    }

    return $views;
}
<?php
add_filter('pre_get_posts', 'my_current_author_posts');

function my_current_author_posts( $query )
{
    global $pagenow;

    // Check if you are on the right Admin page, do nothing if not
    if( 'edit.php' != $pagenow || 'page' != $query->post_type || ! is_admin() ) {
        return;
    }

    // get current user Object
    $current_user = wp_get_current_user();

    // check user capabilities
    if( ! current_user_can( 'edit_others_pages' ) ) {

        // remove views for pages, see the function below
        add_action( 'views_edit-page', 'my_remove_views' );

        // set WP_Query 'Author' parameter
        $query->set( 'author', $current_user->ID );
    }

    return $query;
}

// function to remove views
function my_remove_views( $views )
{

    $views_to_remove = array(
        'all',
        'publish',
        'future',
        'sticky',
        'draft',
        'pending',
        'trash',
    );

    foreach ( $views_to_remove as $view_to_remove ) {
        unset( $views[$view_to_remove] );
    }

    return $views;
}
<?php
add_filter('pre_get_posts', 'my_current_author_posts');

function my_current_author_posts( $query )
{
    global $pagenow;

    // Check if you are on the right Admin page, do nothing if not
    if( 'edit.php' != $pagenow || ! isset( $_GET['post_type'] ) || 'page' != $_GET['post_type'] || ! is_admin() ) {
        return;
    }

    // get current user Object
    $current_user = wp_get_current_user();

    // check user capabilities
    if( ! current_user_can( 'edit_others_pages' ) ) {

        // remove views for pages, see the function below
        add_action( 'views_edit-page', 'my_remove_views' );

        // set WP_Query 'Author' parameter
        $query->set( 'author', $current_user->ID );
    }

    return $query;
}

// function to remove views
function my_remove_views( $views )
{

    $views_to_remove = array(
        'all',
        'publish',
        'future',
        'sticky',
        'draft',
        'pending',
        'trash',
    );

    foreach ( $views_to_remove as $view_to_remove ) {
        unset( $views[$view_to_remove] );
    }

    return $views;
}
Fixed the mistake in the code
Source Link
Max Yudin
  • 6.4k
  • 2
  • 25
  • 36
<?php
add_filter('pre_get_posts', 'my_current_author_posts');

function my_current_author_posts( $query )
{
    global $pagenow;

    // Check if you are on the right Admin page, do nothing if not
    if( 'edit.php' != $pagenow || 'page' != $query->post_type || ! is_admin() ) {
        return;
    }

    // get current user Object
    $current_user = wp_get_current_user();

    // check user capabilities
    if( ! current_user_can( 'edit_others_posts''edit_others_pages' ) ) {

        // remove views for pages, see the function below
        add_action( 'views_edit-page', 'my_remove_views' );

        // set WP_Query 'Author' parameter
        $query->set( 'author', $current_user->ID );
    }

    return $query;
}

// function to remove views
function my_remove_views( $views )
{

    $views_to_remove = array(
        'all',
        'publish',
        'future',
        'sticky',
        'draft',
        'pending',
        'trash',
    );

    foreach ( $views_to_remove as $view_to_remove ) {
        unset( $views[$view_to_remove] );
    }

    return $views;
}
<?php
add_filter('pre_get_posts', 'my_current_author_posts');

function my_current_author_posts( $query )
{
    global $pagenow;

    // Check if you are on the right Admin page, do nothing if not
    if( 'edit.php' != $pagenow || 'page' != $query->post_type || ! is_admin() ) {
        return;
    }

    // get current user Object
    $current_user = wp_get_current_user();

    // check user capabilities
    if( ! current_user_can( 'edit_others_posts' ) ) {

        // remove views for pages, see the function below
        add_action( 'views_edit-page', 'my_remove_views' );

        // set WP_Query 'Author' parameter
        $query->set( 'author', $current_user->ID );
    }

    return $query;
}

// function to remove views
function my_remove_views( $views )
{

    $views_to_remove = array(
        'all',
        'publish',
        'future',
        'sticky',
        'draft',
        'pending',
        'trash',
    );

    foreach ( $views_to_remove as $view_to_remove ) {
        unset( $views[$view_to_remove] );
    }

    return $views;
}
<?php
add_filter('pre_get_posts', 'my_current_author_posts');

function my_current_author_posts( $query )
{
    global $pagenow;

    // Check if you are on the right Admin page, do nothing if not
    if( 'edit.php' != $pagenow || 'page' != $query->post_type || ! is_admin() ) {
        return;
    }

    // get current user Object
    $current_user = wp_get_current_user();

    // check user capabilities
    if( ! current_user_can( 'edit_others_pages' ) ) {

        // remove views for pages, see the function below
        add_action( 'views_edit-page', 'my_remove_views' );

        // set WP_Query 'Author' parameter
        $query->set( 'author', $current_user->ID );
    }

    return $query;
}

// function to remove views
function my_remove_views( $views )
{

    $views_to_remove = array(
        'all',
        'publish',
        'future',
        'sticky',
        'draft',
        'pending',
        'trash',
    );

    foreach ( $views_to_remove as $view_to_remove ) {
        unset( $views[$view_to_remove] );
    }

    return $views;
}
Source Link
Max Yudin
  • 6.4k
  • 2
  • 25
  • 36

<?php
add_filter('pre_get_posts', 'my_current_author_posts');

function my_current_author_posts( $query )
{
    global $pagenow;

    // Check if you are on the right Admin page, do nothing if not
    if( 'edit.php' != $pagenow || 'page' != $query->post_type || ! is_admin() ) {
        return;
    }

    // get current user Object
    $current_user = wp_get_current_user();

    // check user capabilities
    if( ! current_user_can( 'edit_others_posts' ) ) {

        // remove views for pages, see the function below
        add_action( 'views_edit-page', 'my_remove_views' );

        // set WP_Query 'Author' parameter
        $query->set( 'author', $current_user->ID );
    }

    return $query;
}

// function to remove views
function my_remove_views( $views )
{

    $views_to_remove = array(
        'all',
        'publish',
        'future',
        'sticky',
        'draft',
        'pending',
        'trash',
    );

    foreach ( $views_to_remove as $view_to_remove ) {
        unset( $views[$view_to_remove] );
    }

    return $views;
}