0

How can I filter posts in the Back-end so that logged in user sees only her/his own postings and not from others on the listings.

My CPT declaration:

$args = array(
    'labels' => $labels,
    'description' => 'Test plugin.',
    'public' => true,
    'menu_position' => 10,
    'supports' => array('title', 'excerpt', 'comments', 'author'),
    'has_archive' => false,
    'menu_icon' => "dashicons-admin-generic"
);

register_post_type('multi', $args);

1 Answer 1

1

If I am understanding correctly, you want to show only own postings to an user (in the back-end)
I would do it with a seperate function as shown below.

By changing the capability you can decide which users can see all post and which not.
In the function below contributors can't see other users post on the listings in the Back-end.

(please make a backup of the functions.php first ..)
Add this function in your functions.php

/**
 * Show only -own- Posts/CPT to user in the Back-end
 * 
 * Codex links: {@link https://codex.wordpress.org/Plugin_API/Action_Reference/parse_query}
 *              {@link https://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table}
 * 
 * @version Wordpress 4.6.1
 */
add_filter('parse_query', 'wpse241099_display_own_post_only' );
function wpse241099_display_own_post_only( $wp_query ) 
{
    // Check if we are on the correct page (in Back-end)
    if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) 
    {
        // Set capability
        if ( !current_user_can( 'publish_posts' ) ) 
        {
            global $current_user;                   
            $wp_query->set( 'author', $current_user->ID );
        }
    }
} // end function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.