Access control list for posts in WordPress

You can add the code in functions.php, include in plugin or copy file in /mu-plugins/

/*
* Filter posts in SQL
*/
if ( ! function_exists( 'acl_filter_posts_where' ) ) :


  function acl_filter_posts_where($where){

    global $wpdb;

    $current_user_id = get_current_user_id();

    $where .= " AND ($wpdb->posts.post_type != 'post'
                    OR $wpdb->posts.ID IN (
                      SELECT post_id
                      FROM $wpdb->postmeta
                      WHERE $wpdb->postmeta.post_id = $wpdb->posts.ID
                          AND $wpdb->postmeta.meta_key = 'cmdb_acl'
                          AND $wpdb->postmeta.meta_value like '%\"$current_user_id\"%'
                    ))";

    return $where;
  }

  add_filter('posts_where', 'acl_filter_posts_where');

endif;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s