非管理员按标题排除页面

时间:2013-04-16 作者:fightstarr20

我有一个从几个来源构建的代码段,它工作得很好,如果用户不是管理员,它会在编辑页面部分隐藏id为243的页面。。。。

add_action( \'pre_get_posts\' ,\'exclude_pages\' );
function exclude_pages( $query ) {
    if( !is_admin() )
        return $query;

    global $pagenow;
    if( \'edit.php\' == $pagenow && ( get_query_var(\'post_type\') && \'page\' ==     get_query_var(\'post_type\') ) )
        $query->set( \'post__not_in\', array(243) );
    return $query;
}
我真的很想修改它,这样它就可以使用页面标题而不是id,有人能告诉我正确的方向吗?

1 个回复
最合适的回答,由SO网友:tfrommen 整理而成

按标题获取页面,然后使用其ID:

add_action(\'pre_get_posts\', \'exclude_pages\');
function exclude_pages($query) {
    if(! is_admin()) return $query;

    global $pagenow;
    if (\'edit.php\' === $pagenow && (get_query_var(\'post_type\') && \'page\' === get_query_var(\'post_type\'))) {
        // Get the page by its title, ...
        $page = get_page_by_title(\'TITLE_HERE\');
        // then use its ID
        $query->set(\'post__not_in\', array($page->ID));
    }
    return $query;
}
参考文献:get_page_by_title

结束

相关推荐