![](/img/trans.png)
[英]Sort specific Woocommerce product variations by menu_order in a WP_Query
[英]WP_Query ordering by menu_order and not title
我有一個稱為“客戶”的自定義帖子類型。 它具有“ customer_currentpast”屬性,可以是“ current”或“ past”。 客戶與合作伙伴相關。 合作伙伴ID也將傳入。 我想按客戶名稱/標題排序。
該查詢不是按標題排序,而是按menu_order和標題排序。 它不應該按menu_order排序。 我在WP_Query之前立即執行查詢重置,然后在之后進行轉儲,然后按menu_order對結果進行排序,這是錯誤的。 應該是標題。
<?php
wp_reset_query();
$customersCurrentQuery = new WP_Query( array(
'post_type' => 'customer',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'and',
array(
'key' => 'customer_currentpast',
'value' => 'current',
'compare' => '=',
),
array(
'key' => 'partnerid',
'value' => $post->ID,
'type' => 'numeric',
'compare' => '=',
),
) //meta_query
) //args
); //wp_query
?>
這是輸出的查詢
SELECT tableprefix_posts.*
FROM tableprefix_posts
INNER JOIN tableprefix_postmeta ON ( tableprefix_posts.ID = tableprefix_postmeta.post_id )
INNER JOIN tableprefix_postmeta AS mt1 ON ( tableprefix_posts.ID = mt1.post_id )
WHERE 1=1 AND (
( tableprefix_postmeta.meta_key = 'customer_currentpast' AND tableprefix_postmeta.meta_value = 'current' )
AND
( mt1.meta_key = 'partnerid' AND CAST(mt1.meta_value AS SIGNED) = '43' )
) AND tableprefix_posts.post_type = 'customer'
AND (tableprefix_posts.post_status = 'publish' OR tableprefix_posts.post_status = 'private')
GROUP BY tableprefix_posts.ID
ORDER BY tableprefix_posts.menu_order, tableprefix_posts.post_title ASC
將此代碼放在您的functions.php文件中
function filter_query( $query ) {
$query = str_replace("tableprefix_posts.menu_order,", "", $query);
return $query;
}
我們可以在查詢中添加參數ignore_custom_sort = true
因此,在您的情況下,請輸入以下參數。
$customersCurrentQuery = new WP_Query( array(
'post_type' => 'customer',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'and',
array(
'key' => 'customer_currentpast',
'value' => 'current',
'compare' => '=',
),
array(
'key' => 'partnerid',
'value' => $post->ID,
'type' => 'numeric',
'compare' => '=',
),
) //meta_query
) //args
); //wp_query
$query = new WP_Query( $customersCurrentQuery );
add_filter( 'posts_orderby', 'filter_query',99,1 )
如果這對您沒有幫助,請與您分享您使用過的插件,並給我列出它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.