我正在为客户端构建一个站点,我们正在使用WordPress的本机密码保护功能。
是否有筛选器或某种操作来筛选管理面板中检索到的帖子?如果我想阻止所有受密码保护的帖子显示在管理面板中,那么假设您不是管理员。
我正在为客户端构建一个站点,我们正在使用WordPress的本机密码保护功能。
是否有筛选器或某种操作来筛选管理面板中检索到的帖子?如果我想阻止所有受密码保护的帖子显示在管理面板中,那么假设您不是管理员。
您可以使用has_password
的参数WP_Query
.
下面是一个示例,您可以在edit.php
帖子类型的屏幕:
/**
* Hide password protected posts, for non-admins, in the case of \'edit-post\' screen id
*
* @link http://wordpress.stackexchange.com/a/200426/26350
*/
add_action( \'pre_get_posts\', function( \\WP_Query $q )
{
$screen = get_current_screen();
if( is_admin()
&& ! current_user_can( \'manage_options\' )
&& is_a( $screen, \'\\WP_Screen\' )
&& \'edit-post\' === $screen->id
)
$q->set( \'has_password\', false );
} );
希望您可以根据自己的需要进行调整。附言:有一个get_current_screen_id()
函数,类似于get_current_user_id()
函数;-)
/**
* Hide password protected posts, for non-admins, in the case of \'edit-post\' screen id.
* Here we don\'t restrict this to the current user.
*
* @link http://wordpress.stackexchange.com/a/200426/26350
*/
is_admin() && add_action( \'posts_where\', function( $where, \\WP_Query $q )
{
$screen = get_current_screen();
if(
! current_user_can( \'manage_options\' )
&& is_a( $screen, \'\\WP_Screen\' )
&& \'edit-post\' === $screen->id
){
global $wpdb;
$uid = get_current_user_id();
$where .= $wpdb->prepare(
" AND ( ( {$wpdb->posts}.post_password = \'\' AND {$wpdb->posts}.post_author != %d )
OR ( {$wpdb->posts}.post_author = %d ) ) ",
$uid,
$uid
);
}
return $where;
}, 10, 2 );
WP_Query
将支持field_query
:WP_Query
将支持field_query
输入参数,以便更轻松地使用post字段。下面是一个示例:
add_action( \'pre_get_posts\', function( \\WP_Query $q )
{
$screen = get_current_screen();
if( is_admin()
&& ! current_user_can( \'manage_options\' )
&& is_a( $screen, \'\\WP_Screen\' )
&& \'edit-post\' === $screen->id
)
$q->set( \'field_query\',
[
\'outer_relation\' => \'AND\',
\'relation\' => \'OR\',
[
\'field\' => \'author\',
\'value\' => get_current_user_id(),
\'compare\' => \'=\',
],
[
\'relation\' => \'AND\',
[
\'field\' => \'author\',
\'value\' => get_current_user_id(),
\'compare\' => \'!=\',
],
[
\'field\' => \'password\',
\'value\' => \'\',
\'compare\' => \'=\',
],
]
]
);
} );
我有这样一个插件的草稿,所以当它达到alpha阶段时,也许我会把它发布在这里;-)我在get\\u帖子中有一个元查询,它需要花很长时间才能完成。它工作得很好,但只是时间太长了。我有一个名为event. 在每个event 发布后,有自定义元数据:post\\U sort\\U日期(事件日期YmdHis 格式,用于排序)我需要做的是获取下一个事件,该事件相对于$year 和$month 变量。所以如果$year = 2021 和$month = 10 (2021 10月)然后应该在2021 11月或之后找到第一个事件。我下面的查询很好,但很慢。执行大约需要40秒,我不知道为什么。$next