当我有复杂的查询要做时,我经常使用预查询来获得良好的post\\u id。然后,我通过post__in
parameter of WP_Query.
$meta_key = \'RepeaterName_%_FieldName\';
$meta_value = \'MyCustomValue\';
global $wpdb;
$post_ids = $wpdb->get_col( $wpdb->prepare(
"
SELECT pm.post_id
FROM $wpdb->postmeta pm
WHERE pm.meta_key LIKE %s
AND pm.meta_value = %s
",
$meta_key,
$meta_value,
) );
// Or with another custom $wpdb request
// https://developer.wordpress.org/reference/classes/wpdb/
然后我们添加
$post_ids
至主查询
if ( !empty( $post_ids ) ) {
$post__in = $query->get(\'post__in\');
if ( empty( $post_in ) ) {
$query->set( \'post__in\', $post_ids );
} else {
$query->set( \'post__in\', array_intersect( $post_ids, $post__in ) )
}
}
所有这些代码必须放在
pre_get_posts
滤器我希望回复达到您的期望。