您可以使用过滤器修改SQL。我在post\\u类型中添加了一些独特的内容,因此我只能过滤此查询,而不能更改任何其他内容。
例如,查询
$query = new WP_Query(array(\'post_type\'=>\'something_unique_for_filter\'));
和过滤器
function custom_where($where, $query) {
global $wpdb;
if(false !== strpos($where, \'something_unique_for_filter\')) {
$where = " AND post_type=\'page\' AND post_parent != 0 AND ID IN (SELECT DISTINCT post_parent FROM $wpdb->posts WHERE post_type=\'page\')";
}
return $where;
}
add_filter(\'posts_where\', \'custom_where\', 10, 2);
要添加对元查询的支持,第一步是添加
meta_key
到
WP_Query
因此,它内部连接Posteta表。
$query = new WP_Query(array(\'post_type\'=>\'something_unique_for_filter\', \'meta_key\'=>\'something\'));
和带meta的过滤器
function custom_where($where, $query) {
global $wpdb;
if(false !== strpos($where, \'something_unique_for_filter\')) {
$where = " AND ( $wpdb->postmeta.meta_key = \'test\' AND $wpdb->postmeta.meta_value = \'123\' )
AND post_type=\'page\' AND post_parent != 0
AND ID IN (SELECT DISTINCT post_parent FROM $wpdb->posts WHERE post_type=\'page\')";
}
return $where;
}
add_filter(\'posts_where\', \'custom_where\', 10, 2);