在撰写本文时,还没有元查询compare
可以做你想做的事情的价值观,基本上是其中(<);更新了meta上的\\u>&燃气轮机<;build\\U ran\\U at meta>“引用”;。但是,除了使用meta_query
参数:
使用原始SQL仅检索具有updated_at
元大于build_ran_at
然后将ID传递给WP_Query
通过post__in
arg like so:
// Build the raw SQL.
$query = "
SELECT p.ID
FROM $wpdb->posts p
INNER JOIN $wpdb->postmeta pm ON pm.post_id = p.ID
INNER JOIN $wpdb->postmeta pm2 ON pm2.post_id = p.ID
WHERE p.post_type = \'foobar\'
AND p.post_status = \'publish\'
AND pm.meta_key = \'updated_at\'
AND pm2.meta_key = \'build_ran_at\'
AND pm.meta_value > pm2.meta_value
LIMIT 999
";
// Get the post IDs.
$ids = $wpdb->get_col( $query );
// Then use the IDs as the post__in value.
$foobar_query = new WP_Query([
\'post_type\' => \'foobar\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 999,
\'post__in\' => $ids,
]);
或使用
posts_clauses
hook 在WHERE子句中添加上述两个JOIN子句和最后三个条件。
使用(闭包和)名为_updated_at
作为指示是否应过滤POST查询子句的标志,以避免其他WP_Query
受影响的查询:
// Add the filter.
add_filter( \'posts_clauses\', function ( $clauses, $query ) {
if ( \'> build_ran_at\' === $query->get( \'_updated_at\' ) ) {
global $wpdb;
$pm = uniqid( \'pm_\' ); // unique table alias
$clauses[\'join\'] .= " INNER JOIN $wpdb->postmeta $pm ON {$pm}.post_id = {$wpdb->posts}.ID";
$pm2 = uniqid( \'pm_\' ); // unique table alias
$clauses[\'join\'] .= " INNER JOIN $wpdb->postmeta $pm2 ON {$pm2}.post_id = {$wpdb->posts}.ID";
$clauses[\'where\'] .= " AND ( {$pm}.meta_key = \'updated_at\' AND {$pm2}.meta_key = \'build_ran_at\'"
"AND {$pm}.meta_value > {$pm2}.meta_value )";
}
return $clauses;
}, 10, 2 );
// Then use the _updated_at arg in place of meta_query.
$foobar_query = new WP_Query([
\'post_type\' => \'foobar\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 999,
\'_updated_at\' => \'> build_ran_at\',
]);