admin管理员组文章数量:1291023
I've been looking for a plugin that works like a private journal and had no luck. The closest I found so far was this guide from buddyforms. In terms of functionality, this is pretty close to what I need. I tried it on a setup with Buddyboss instead but the code somehow affects the menu and hides them completely. I tried in on a bare WP 2021 theme and it seems to be working but when I start working on a premade theme, issues start coming up.
add_action( 'pre_get_posts', 'buddyforms_filter_cpt_listing_by_author' );
function buddyforms_filter_cpt_listing_by_author( $wp_query_obj ) {
// First let us check if this is a page. We not want to restrict pages
if( !isset( $wp_query_obj->query ) || isset( $wp_query_obj->query['page'] ) || 'page' == $wp_query_obj->query['post_type'] ){
return $wp_query_obj;
}
// Check if the user is logged in and display a page to logged off users
// Just change the page_id to the page you want to display
// If you want to only restrict a custom post type, move this block to line 23
if( ! is_user_logged_in() ) {
$wp_query_obj->set('post_type', 'page' );
$wp_query_obj->set('page_id', '2' );
return $wp_query_obj;
}
// If you want to have the Author Only Posts work only with a custom post type uncomment this 3 lines and change "custom post type" to your post type.
// if( ! isset( $wp_query_obj->query['post_type'] ) && 'custom post type' != $wp_query_obj->query['post_type'] ){
// return $wp_query_obj;
// }
// Let us get the logged in user
$current_user = wp_get_current_user();
// If the user is not administrator or can at lest delete posts show all posts
if( !current_user_can( 'delete_plugins' ) ){
return $wp_query_obj->set('author', $current_user->ID );
}
}
本文标签: Hiding posts by other users and nonlogged in
版权声明:本文标题:Hiding posts by other users and non-logged in 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741523127a2383306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论