Sorting 如何在商业中按自定义分类法订购产品?

Sorting 如何在商业中按自定义分类法订购产品?,sorting,woocommerce,custom-taxonomy,Sorting,Woocommerce,Custom Taxonomy,我花了很长时间寻找解决问题的方法,但似乎找不到(或者可能我不太理解我在这里找到的一些帖子) 问题是,我已经在我的网站上创建了一个自定义标签,它是“marca”(标签,英文)。我的客户希望按照这个标签(“marca”)和字母顺序对她的所有产品进行排序。但Woocommerce默认情况下会按标题、日期、价格对产品进行排序。。。你知道,但是如果我想定制一个排序,这是非常困难的 我试过这个论坛上的一些代码,但不幸的是它们根本不起作用。有可能做到这一点吗 谢谢。我为您制作了一个带有解释的WP\u查询示例,

我花了很长时间寻找解决问题的方法,但似乎找不到(或者可能我不太理解我在这里找到的一些帖子)

问题是,我已经在我的网站上创建了一个自定义标签,它是“marca”(标签,英文)。我的客户希望按照这个标签(“marca”)和字母顺序对她的所有产品进行排序。但Woocommerce默认情况下会按标题、日期、价格对产品进行排序。。。你知道,但是如果我想定制一个排序,这是非常困难的

我试过这个论坛上的一些代码,但不幸的是它们根本不起作用。有可能做到这一点吗


谢谢。

我为您制作了一个带有解释的WP\u查询示例,它返回分类为“marca”的类型为“product”的帖子,这些帖子的术语按字母顺序设置

$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();
    }
}

编辑: 下面是可以粘贴到代码段中的代码。它钩住
woocommerce\u product\u查询
,并更改所有归档页面上的查询

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 );

您实际使用什么代码来完成排序?您需要的是一个分类查询:@jjj我尝试了各种代码,但都没有成功。我想我做错了什么。例如,来自这个威胁:或者这个威胁:我正在使用代码片段插件来介绍代码(不是int functions.php)。@DanielVisser谢谢,但我对这种信息感到迷茫。我知道的代码太少了,所以我不太懂。。。很抱歉所以,如果有人能告诉我如何做到这一点,我请求帮助:(我用一个例子发布了我的答案。