Created
December 24, 2016 21:32
-
-
Save eduardopintor/d670b38982c3b5c69ad4f892f7ebaf81 to your computer and use it in GitHub Desktop.
Filter Wordpress Posts by User Meta (Custom Post Type Taxonomy)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'posts_where' , 'me_posts_where' ); | |
function me_posts_where( $where ) { | |
if( is_admin() ) { | |
global $wpdb; | |
// Get Current Page | |
global $pagenow; | |
// Get Current Logged In User | |
global $current_user; | |
get_currentuserinfo(); | |
if( $pagenow=='edit.php' ) { | |
// Get User ID | |
$user_id = $current_user->data->ID; | |
// Get User Department using ACF API for get_field | |
$user_department = get_field('user_department', 'user_' . $user_id); | |
// If no Department has been assigned, return entire Feedback (for main admin) | |
if($user_department != '') { | |
$where .= " AND ID IN (SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id=$user_department )"; | |
} | |
} | |
} | |
return $where; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment