我有以下查询,我通过以下方式在我的taxonomy.php模板中调用 query_brands_geo('dealers', 'publish', '1', $taxtype, $geo, $brands);
此功能运行完美。但是,在阅读了用于查询帖子的Codex之后,它提到了pre_get_posts作为更改默认查询的首选方法。pre_get_posts比下面的wp_query函数更有效吗?
如果是这样,我将如何构造pre_get_posts并在下面传递变量和查询?
function my_custom_query($posttype, $poststatus, $paidvalue, $taxtype, $geo, $brands) {
global $wp_query;
$wp_query = new WP_Query();
$args = array(
'post_type' => $posttype,
'post_status' => array($poststatus),
'orderby' => 'rand',
'posts_per_page' => 30,
'meta_query' => array(
array(
'key' => 'wpcf-paid',
'value' => array($paidvalue),
'compare' => 'IN',
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $taxtype,
'field' => 'slug',
'terms' => $geo
),
array(
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $brands
)
)
);
return $wp_query->query($args);
}