我为您做了一个 WP_Query 示例,它会返回类型为“product”的帖子,分类为“marca”,术语按字母顺序排列。
$query_args = array(
'post_type' => 'product', // The post type we want to query, in our case 'product'.
'orderby' => 'title', // Return the posts in an alphabetical order.
'order' => 'ASC', // Return the posts in an ascending order.
'posts_per_page' => -1, // How many post we want to return per page, -1 stands for no limit.
'tax_query' => array( // Taxonomy query
array(
'taxonomy' => 'marca', // The taxonomy we want to query, in our case 'marca'.
'operator' => 'EXISTS' // Return the post if it has the selected taxonomy with terms set, in our case 'marca'.
)
)
);
$query = new WP_Query( $query_args );
if( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo the_title();
}
}
编辑:
这是您可以粘贴到代码 sn-p 中的代码。它挂钩woocommerce_product_query 并更改所有存档页面上的查询。
function custom_woocommerce_product_query( $query ){
$query->set( 'orderby', 'title' ); // Return the posts in an alphabetical order.
$query->set( 'order', 'ASC' ); // Return the posts in an ascending order.
$tax_query = array(
'taxonomy' => 'marca', // The taxonomy we want to query, in our case 'marca'.
'operator' => 'EXISTS' // Return the post if it has the selected taxonomy with terms set, in our case 'marca'.
);
$query->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_woocommerce_product_query', 10, 1 );