Tagged: , ,

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1462105

    Hello,

    I have foreseen that several users can insert posts to be checked before publication, which is why I am using the backend function to add a new post without any particular problems. Of course it would be nice to be able to provide the function from the frontend, but a plugin like https://it.wordpress.org/plugins/user-submitted-posts/ could not use avia editor to create the post and this would be very inconvenient.

    However, I would also like to allow a user logged into the site to be able to view the list of posts he has created, unfortunately I can limit the posts by default by category or by role using the “user role editor” plugin, but not by current user.
    Can we find a way to do so, even without providing very sophisticated visualizations?

    Thanks for your support
    Andrea

    #1462222

    Hey cuccarini,

    Thank you for the inquiry.

    You can use this filter in the functions.php file to display only posts created by the active user:

    function av_filter_posts_by_current_user($query) {
        if (is_admin() && $query->is_main_query() && $query->get('post_type') === 'post' && current_user_can('edit_posts')) {
            $current_user_id = get_current_user_id();
            $query->set('author', $current_user_id);
        }
    }
    
    add_action('pre_get_posts', 'av_filter_posts_by_current_user');
    

    Best regards,
    Ismael

    #1462229

    Thanks Ismael,

    for displaying posts, would it be optimal to use a shortcode to insert into a block in the frontend or is it necessary to link the post management function in the backend?
    I imagine the administrator continues to see all the posts right?

    Thanks again for tour support
    Andrea

    #1462236

    PS This is not the topic, but I found problems in the block editor with wordpress 6.6. I was obliged to downgrade to wordpress 6.5.5 that is OK.

    • This reply was modified 4 months, 1 week ago by cuccarini.
    #1462350

    Hi,

    Thank you for the update.

    I imagine the administrator continues to see all the posts right?

    When the hook above is used, only posts created by the active user are displayed, even for admin users. It will only work in the backend or dashboard.

    Best regards,
    Ismael

    #1462378

    Sorry Ismael,

    but in this way I don’t think it’s a reasonable operation, I think a shortcode to use in the frontend with a functionality to display only the articles created by the current user is needed.
    Changing the backend functionality of the WordPress dashboard for everyone is definitely not good.
    Please let me know if there is an alternative in this sense or if it is necessary to create a custom function.

    Have a nice day
    Andrew

    #1462386

    Hello Ismael,

    I have solved the problem using the following code in my funcion.php in enfold child theme!
    In this way the user is limited only to see and update his posts while the admin and editor can mange all posts.

    function posts_for_current_author($query) {
    global $pagenow;

    if( ‘edit.php’ != $pagenow || !$query->is_admin )
    return $query;

    if( !current_user_can( ‘edit_others_posts’ ) ) {
    global $user_ID;
    $query->set(‘author’, $user_ID );
    }
    return $query;
    }
    add_filter(‘pre_get_posts’, ‘posts_for_current_author’);

    #1462387

    You can close the ticket now, thanks

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Display only of posts created by the current user’ is closed to new replies.