您可以使用过滤器pre_get_posts 操纵全局WP_Query 对象应用过滤器。
如果您的过滤器添加URL参数,您可以操纵查询并根据自己的喜好进行调整。
这里有一个例子。您仍然需要调整分类法和帖子类型以使其适合您。
<?php // functions.php
/**
* Modifies the query in a Post Type Archive and defines the taxonomy for hair color.
*
* @param WP_Query $query The global WP_Query Object.
*
* @return WP_Query
*/
function my_custom_query( $query ) {
/**
* Here we tell WordPress that we want to adjust the query when we
* are not in the admin area and only on the post type archive for girls.
*/
if ( ! is_admin() && is_post_type_archive( \'girl\' ) ) {
/**
* Here we check whether a query parameter hair_color is available and then
* apply it. To do this, we change the taxonomy of the current query.
*/
if ( isset( $_GET[\'hair_color\'] ) && \'\' !== $_GET[\'hair_color\'] ) {
$hair_color = sanitize_text_field( wp_unslash( $_GET[\'hair_color\'] ) );
/**
* Here we define the term hair_color for the taxonomy hair_colors.
*/
$hair_taxonomy = [
[
\'taxonomy\' => \'hair_colors\',
\'field\' => \'slug\',
\'terms\' => $hair_color,
],
];
$query->set( \'tax_query\', $hair_taxonomy );
}
}
/**
* Here we return the manipulated query.
*/
return $query;
}
/**
* Here our manipulated query is transferred to WordPress.
*/
add_filter( \'pre_get_posts\', \'my_custom_query\' );