My posts_results
filter is causing memory errors and I can't figure out why. Here's the full snippet:
/*-----------------------------------------------------------------------------------*/
/* Filter posts after they are returned */
/*-----------------------------------------------------------------------------------*/
function my_posts_results_filter( $posts ) {
$filtered_posts = array();
//printr($posts);
foreach ( $posts as $post ) {
$post_type = get_post_type();
if ( $post_type != 'post' ) {
$filtered_posts[] = $post;
continue;
}
//Who can see this?
$whosee = get_field('who_can_view',$post->ID);
$post_meta = get_post_meta($post->ID);
//printr($post_meta);
//What is this user's role?
$user_id = get_current_user_id();
$user_info = get_userdata($user_id);
$user_role = $user_info->roles[0];
if ( $user_role == 'subscriber' || $user_role == 'author' || $user_role == 'contributor' || $user_role == 'editor' ) $user_role = 'member';
if ( $whosee != "" ) {
if ( $user_role == $whosee || $user_role == 'administrator' ) {
$filtered_posts[] = $post;
}
}
}
return $filtered_posts ;
}
if ( !is_admin() ) {
add_filter( 'posts_results', 'my_posts_results_filter' );
}
Any thoughts would be great.
Thanks, Jay
get_field
andget_post_meta
for example. But exactly what are you trying to accomplish, in words? Looking at the code, my gut tells me that this is not the way to go.pre_get_posts
later on and forgot to disable this filter (stupid and sloppy on my part). This code was making WAY too many requests to the database. I've now turned it off and have used pre_get_posts to filter posts out prior to making the queries. Thanks for taking the time to comment.