现在它只适用于过滤器“posts\\u groupby”、“posts\\u where”和“posts\\u join”<如果您知道更好的解决方案,请写信。
$args = array(
"post_type" => array("product", "post", "page", "topic"),
"posts_per_page" => $nimbus_posts_on_home,
\'category__not_in\' => array( 124 /*Gallery*/, 52 /* Discounts */ ),
);
add_filter( \'posts_groupby\', \'recent_posts_widget_groupby\' );
add_filter( \'posts_where\', \'recent_posts_widget_where\', 10, 2 );
add_filter( \'posts_join\', \'recent_posts_widget_join\', 10, 1 );
function recent_posts_widget_groupby( $groupby ) {
remove_filter( \'posts_groupby\', \'recent_posts_widget_groupby\' );
if( strpos($groupby, \'wp_posts.ID\' ) === false )
$groupby .= " wp_posts.ID ";
return $groupby;
}
function recent_posts_widget_join( $join ){
remove_filter( \'posts_join\', \'recent_posts_widget_join\' );
$join .= " INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) ";
return $join;
}
function recent_posts_widget_where( $where, &$wp_query ) {
remove_filter( \'posts_where\', \'recent_posts_widget_where\' );
$post_types = array();
foreach( $wp_query->query[\'post_type\'] as $post_type) {
if( $post_type != \'topic\')
$post_types[] = $post_type;
}
$post_type_in = implode("\',\'", $post_types );
$where = " AND(
wp_posts.post_type IN ( \'$post_type_in\' )
OR
(
wp_posts.post_type=\'topic\' AND( mt1.meta_key = \'include_newsletter_feed\' AND mt1.meta_value = \'true\' )
)
) ". $where;
return $where;
}
$wp_query = new WP_Query($args);
最后的SQL查询如下所示
SELECT * FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1 AND(
wp_posts.post_type IN ( \'post\',\'page\',\'product\' )
OR
(
wp_posts.post_type=\'topic\' AND( mt1.meta_key = \'include_newsletter_feed\' AND mt1.meta_value = \'true\' )
)
) AND (
wp_posts.ID NOT IN (
SELECT object_id
FROM wp_term_relationships
WHERE term_taxonomy_id IN (164,142)
)
) AND wp_posts.post_type IN (\'post\', \'page\', \'topic\', \'product\') AND ((wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'closed\'))
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 3