skip to Main Content

I have a website with multiple authors and I want to those authors could only see their own added posts and posts that have some specific custom field.

So I used this code for restriction at the first step and worked, but this code restrict authors to see only their own added posts:

function pposts_for_current_author($query) {
    global $pagenow, $parent_file, $post_type;
    $user = wp_get_current_user();
    
    
    if('movie' == $post_type || 'edit.php' != $pagenow || !$query->is_admin ) {
        return $query;
    }

if(!current_user_can( 'edit_user', $user_id )) {
        global $user_ID;
        $query->set('author', $user_ID );
    }
    
    return $query;
}
add_filter('pre_get_posts', 'pposts_for_current_author');

Then I just combined this code with get_post_meta function:

function pposts_for_current_author($query) {
    global $pagenow, $parent_file, $post_type;
    $user = wp_get_current_user();
    $moviePublic = get_post_meta($post->ID, 'movie_multi_author', true);
    
    if('movie' == $post_type || 'edit.php' != $pagenow || !$query->is_admin ) {
        return $query;
    }

    if(!current_user_can( 'edit_user', $user_id ) && $moviePublic == 1) {
        global $user_ID;
        $query->set('author', $user_ID );
    }

    return $query;
}
add_filter('pre_get_posts', 'pposts_for_current_author');

I don’t exactly what should I doing with this code. Anyone could make it right please? Thanks.

2

Answers


  1. It looks like you’re trying to restrict authors on your WordPress website to see only their own posts and posts with a specific custom field. The code you provided seems to be on the right track, but there are a few issues and improvements needed. Here’s a revised version of your code:

    function restrict_posts_for_current_author($query) {
        global $pagenow, $post_type;
    
        // Check if we are on the 'edit.php' page for your custom post type 'movie'
        if ($pagenow == 'edit.php' && $post_type == 'movie' && !current_user_can('manage_options')) {
            $user_id = get_current_user_id();
    
            // Modify the query to show only posts authored by the current user
            $query->set('author', $user_id);
    
            // Additionally, check for a custom field 'movie_multi_author'
            // If set to 1, allow the user to see those posts
            $query->set('meta_key', 'movie_multi_author');
            $query->set('meta_value', 1);
        }
    
        return $query;
    }
    add_filter('pre_get_posts', 'restrict_posts_for_current_author');
    

    Here’s what’s changed or added:

    1. Removed the unnecessary global variables $parent_file and $user.
    2. Checked if the current user has the ‘manage_options’ capability. If they do, they can see all posts (admin). If not, the code restricts the query to posts authored by the current user and those with the custom field ‘movie_multi_author’ set to 1.
    3. Used get_current_user_id() to get the current user’s ID.

    With these changes, this code should work to restrict authors to see their own posts and posts with the specified custom field. Just make sure you have the custom field ‘movie_multi_author’ set correctly for the posts you want to be visible to multiple authors.

    Login or Signup to reply.
  2. I understand your concern. If you want the query to work with both the author restriction and the custom meta_key, you can modify the code like this:

    function restrict_posts_for_current_author($query) {
        global $pagenow, $post_type;
    
        // Check if we are on the 'edit.php' page for your custom post type 'movie'
        if ($pagenow == 'edit.php' && $post_type == 'movie' && !current_user_can('manage_options')) {
            $user_id = get_current_user_id();
    
            // Use 'relation' => 'OR' to combine author and meta_key queries
            $query->set('author', $user_id);
            $query->set('meta_query', array(
                'relation' => 'OR',
                array(
                    'key' => 'movie_multi_author',
                    'value' => 1,
                    'compare' => '='
                ),
                // Add any other meta queries you need here
            ));
        }
    
        return $query;
    }
    add_filter('pre_get_posts', 'restrict_posts_for_current_author');
    

    This code uses the 'relation' => 'OR' parameter within the meta_query array to combine the author query and the custom meta_key query. This way, it should retrieve posts authored by the current user and posts with the specified custom field ‘movie_multi_author’ set to 1. You can also add additional meta queries as needed within the meta_query array if you have other conditions to check.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search