我有以下代碼:
function filter_by_user( $query ) {
$user = get_current_user_id();
if ( ! $meta_query ) {
$meta_query = [];
}
// Append our meta query
$meta_query[] = [
'key' => 'id_customerJK',
'value' => $user,
'compare' => 'LIKE',
];
$query->set( 'meta_query', $meta_query );
}
add_action( 'pre_get_posts', 'filter_by_user', 9998 );
它作業得很好,但是它在每一頁上都會執行。我希望它只在自定義帖子型別“customer_bill”單數中執行。
有可能這樣做嗎?
uj5u.com熱心網友回復:
您可以使用以下條件來更改查詢:
is_singleDocs函式檢查查詢是否針對現有的單個帖子。
和
get('post_type')在“$query”變數上使用方法。
add_action('pre_get_posts', 'filter_by_user', 9998);
function filter_by_user($query)
{
if (
is_single()
&&
'customer_bill' == $query->get('post_type')
)
{
$user = get_current_user_id();
$meta_query = $meta_query ?: [];
$meta_query[] = [
'key' => 'id_customerJK',
'value' => $user,
'compare' => 'LIKE',
];
$query->set('meta_query', $meta_query);
}
}
uj5u.com熱心網友回復:
嘗試 :
if ( is_singular('customer_bill') ) {
// Your Code
或者
if ( ! is_singular('customer_bill') ) {
return;
}
// Your Code
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/414434.html
標籤:
