我通过使用动作挂钩Custom Post Type
自定义了对特定pre_get_posts
的搜索。它工作正常,但问题在于,它也在wp-admin
搜索帖子中得到应用。
function basket_pre_get_posts($query) {
if( is_search() && $query->is_main_query() ) {
$query->set('post_type', 'basket');
}
}
add_action( 'pre_get_posts', 'basket_pre_get_posts' );
自定义帖子类型:basket
答案 0 :(得分:3)
您应将此条件设置为:
if( is_admin() ) {
return;
}
如果是管理员,则不应用就返回。
function basket_pre_get_posts($query) {
if( is_admin() ) {
return;
}
if( is_search() && $query->is_main_query() ) {
$query->set('post_type', 'basket');
}
}
add_action( 'pre_get_posts', 'basket_pre_get_posts' );
答案 1 :(得分:1)
在函数中添加is_admin条件,它将停止函数在管理区域中运行
function basket_pre_get_posts($query) {
if ( !is_admin() ) {
if( is_search() && $query->is_main_query() ) {
$query->set('post_type', 'basket');
}
}
}
add_action( 'pre_get_posts', 'basket_pre_get_posts' );
答案 2 :(得分:0)
You just need to check `is_admin` before applying filter.
function basket_pre_get_posts($query) {
if( is_admin() ) {
return;
}
if( is_search() && $query->is_main_query() ) {
$query->set('post_type', 'basket');
}
}
add_action( 'pre_get_posts', 'basket_pre_get_posts' );