我没有要测试的数据,因此下面的代码可能是错误的。
add_filter(\'posts_search\', \'set_is_tax_to_true\' ,10,2);
function set_is_tax_to_true($search,$query){
$query->is_tax = true;
}
$args = array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => -1,
\'post_status\' => \'pending\',
\'post_parent\' => $post_id,
\'order\' => \'ASC\',
\'orderby\' => \'ID\',
\'meta_query\'=> array(
array(
\'key\' => \'cp_sys_expire_date\',
\'value\' => current_time(\'mysql\'),
\'compare\' => \'>\',
\'type\' => \'DATE\'
)
));
$query = new WP_Query($args);
$images = $query->get_posts();
remove_filter(\'posts_search\',\'set_is_tax_to_true\',10);
它将生成类似SQL的
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts LEFT JOIN wp_posts AS p2 ON (wp_posts.post_parent = p2.ID) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = \'attachment\' AND ((wp_posts.post_status = \'pending\') OR (wp_posts.post_status = \'inherit\' AND (p2.post_status = \'pending\'))) AND ( (wp_postmeta.meta_key = \'cp_sys_expire_date\' AND CAST(wp_postmeta.meta_value AS DATE) > \'2013-04-08 10:11:09\') ) GROUP BY wp_posts.ID ORDER BY wp_posts.ID
嗯,很长的SQL。
这个set_is_tax_to_true
hack是强制WP\\u Query生成此((wp_posts.post_status = \'pending\') OR (wp_posts.post_status = \'inherit\' AND (p2.post_status = \'pending\')))
注:我假设post meta中存储的日期格式为2012-12-12 12:12:12
. 如果没有,请按此操作link 更改meta\\u查询参数