【发布时间】:2020-08-18 12:46:27
【问题描述】:
我正在尝试最后整理我缺货的产品。 它有效,但我通过 DACrosby 找到的查询也针对自定义帖子类型。 Show Out of stock products at the end in Woocommerce
这是原始代码:
add_filter('posts_clauses', 'order_by_stock_status');
function order_by_stock_status($posts_clauses) {
global $wpdb;
// only change query on WooCommerce loops
if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {
$posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
$posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
$posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
}
return $posts_clauses;
}
当我监控我的查询时,我看到了这个:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_relationships AS tt1
ON (wp_posts.ID = tt1.object_id)
LEFT JOIN wp_icl_translations wpml_translations
ON wp_posts.ID = wpml_translations.element_id
AND wpml_translations.element_type = CONCAT('post_', wp_posts.post_type)
INNER JOIN wp_postmeta istockstatus
ON (wp_posts.ID = istockstatus.post_id)
WHERE 1=1
AND istockstatus.meta_key = '_stock_status'
AND istockstatus.meta_value <> ''
OR wp_posts.post_type = 'gp_elements'
AND ( wp_term_relationships.term_taxonomy_id IN (1816)
AND wp_posts.ID NOT IN (
SELECT object_id
FROM wp_term_relationships
WHERE term_taxonomy_id IN (10) )
AND tt1.term_taxonomy_id IN (1816) )
AND wp_posts.post_type = 'product'
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private')
AND ( ( ( wpml_translations.language_code = 'nl'
OR ( wpml_translations.language_code = 'nl'
AND wp_posts.post_type IN ( 'gp_elements' )
AND ( ( (
SELECT COUNT(element_id)
FROM wp_icl_translations
WHERE trid = wpml_translations.trid
AND language_code = 'nl' ) = 0 )
OR ( (
SELECT COUNT(element_id)
FROM wp_icl_translations t2 JOIN wp_posts p
ON p.id = t2.element_id
WHERE t2.trid = wpml_translations.trid
AND t2.language_code = 'nl'
AND ( p.post_status = 'publish'
OR p.post_type='attachment'
AND p.post_status = 'inherit' ) ) = 0 ) ) ) )
AND wp_posts.post_type IN ('post','page','attachment','wp_block','product','product_variation','gp_elements' ) )
OR wp_posts.post_type NOT IN ('post','page','attachment','wp_block','product','product_variation','gp_elements' ) )
GROUP BY wp_posts.ID
ORDER BY istockstatus.meta_value ASC, wp_posts.menu_order ASC, wp_posts.post_title ASC
LIMIT 0, 20
如您所见,它还以 gp_elements 为目标,这是我的主题的自定义帖子类型。 由于 where 不包含它,因此它不会显示在我的页面上。
任何提示如何将其重新包含在我的页面上并保持排序?
【问题讨论】:
标签: php sql wordpress woocommerce