-
AuthorPosts
-
July 15, 2024 at 6:38 pm #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
AndreaJuly 17, 2024 at 8:43 am #1462222Hey 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,
IsmaelJuly 17, 2024 at 10:04 am #1462229Thanks 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
AndreaJuly 17, 2024 at 11:07 am #1462236PS 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 5 months, 1 week ago by cuccarini.
July 18, 2024 at 7:50 am #1462350Hi,
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,
IsmaelJuly 18, 2024 at 11:54 am #1462378Sorry 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
AndrewJuly 18, 2024 at 12:35 pm #1462386Hello 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’);July 18, 2024 at 12:36 pm #1462387You can close the ticket now, thanks
-
AuthorPosts
- The topic ‘Display only of posts created by the current user’ is closed to new replies.