POST_RESULTS过滤器函数内存错误

时间:2015-05-18 作者:jay

我的posts_results 过滤器导致内存错误,我不知道为什么。以下是完整片段:

/*-----------------------------------------------------------------------------------*/
/* 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\' );
}
任何想法都会很好。

谢谢Jay

1 个回复
SO网友:jay

抱歉,各位,对于那些好奇的人,我已经在稍后的函数中解决了这个问题。php文件,忘记关闭此过滤器(愚蠢)。如果有人想评论或改进,这里有一个更好的解决方案。基本上,我是根据用户角色(我也有一个自定义用户角色)过滤内容的,这样帖子就不会显示给不允许查看的用户。这必须包括侧边栏小部件(例如,post archive listings)和您可能看到文章标题或链接的所有其他位置:

/*-----------------------------------------------------------------------------------*/
/* Filter out users who shouldn\'t see content */
/*-----------------------------------------------------------------------------------*/
function filter_users($query){

    if ( !is_admin() ) {

            if( is_page() ) { return; }

            $user_id    = get_current_user_id();
            $user_info  = get_userdata($user_id);
            $user_role  = $user_info->roles[0];
            $user_role  = ( $user_role == \'subscriber\' || $user_role == \'author\' || $user_role == \'contributor\' || $user_role == \'editor\' )  ? \'member\' : $user_role;

            if ( $user_role == \'member\' ) {

                //wordpress needs a value to compare or it won\'t work
                $query->set(\'meta_query\', array(
                                            array(
                                                \'meta_key\'=>\'who_can_view\',
                                                \'meta_value\'=>\'foobar\',
                                                \'compare\' => \'NOT EXISTS\',
                                            )   
                                          )
                            );

                add_filter( \'posts_join\' , \'custom_posts_join\' );
                add_filter( \'posts_where\',\'custom_where\' );

            }

    }

}
add_filter(\'pre_get_posts\',\'filter_users\');

function custom_posts_join($join){

    global $wpdb;

    $join .= " LEFT JOIN $wpdb->postmeta as meta_1 ON $wpdb->posts.ID = meta_1.post_id";

    return $join;
}

//Needed because WordPress craps the bed setting meta_query objects in pre_get_posts
function custom_where( $where = \'\' ){

    global $wpdb;

    $where .= " AND ( meta_1.meta_key = \'who_can_view\' AND meta_1.meta_value = \'member\' )";

    remove_filter( \'posts_where\', \'custom_where\' );

    return $where;

} // custom_where
干杯。

结束

相关推荐

在非文档挂接上调用Apply_Filters

我一直在努力学习如何编写插件,我认为最好的方法是查看其他插件。我在看这一行和第一行/** * Plugin Name: Media Library Categories * Plugin URI: http://wordpress.org/plugins/wp-media-library-categories/ * Description: Adds the ability to use categories in the media library. * Vers