Skip to content

Instantly share code, notes, and snippets.

@svenl77
Created August 31, 2017 16:50
Show Gist options
  • Save svenl77/43d3f47960ac380b3102e9f9a173b0b7 to your computer and use it in GitHub Desktop.
Save svenl77/43d3f47960ac380b3102e9f9a173b0b7 to your computer and use it in GitHub Desktop.
<?php
add_action( 'pre_get_posts', 'jim_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', '5455' );
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'] ) && 'my-medications-form' != $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