Skip to content

Instantly share code, notes, and snippets.

@svenl77
Last active August 7, 2017 14:31
Show Gist options
  • Save svenl77/7337f68934b3056fd679d587b7f3845c to your computer and use it in GitHub Desktop.
Save svenl77/7337f68934b3056fd679d587b7f3845c to your computer and use it in GitHub Desktop.
<?php
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 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment