首页 文章

我如何按类别对所有产品进行woocommerce排序

提问于
浏览
3

我有wp_query请求提供所有产品,并需要为2个字段排序:按类别和menu_order . 不同!我需要在每个类别中按“menu_order”排序 .

在简单查询中:

$args = array(
        'orderby' =. 'product_cat menu_order'
        'posts_per_page' => -1,
        'post_type' => 'product',
        );

      $loop = new WP_Query($args);

在全局$ product中,存在字段“menu_order”,但不存在字段“product_cat” .

我可以用wp_query来做吗?或者可能存在另一种方式来做到这一点?

2 回答

  • -1

    我是成立的正确方法,代码吼叫,这个例子

    /* Products Loop */
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'posts_per_page' => $prod_per_page,
                'paged' => $paged,
                'post_type' => 'product',
                'product_cat' => $product_cat,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id',
                        'terms' => $subcats_ar,
                        'operator' => 'NOT IN'
                    ),
                ),
                'orderby' => array('menu_order' => 'ASC', 'title' => 'ASC')
            );
            $loop = new WP_Query($args);
    

    更简单的方法,没有分页和排除子类别:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $args = array(
                'posts_per_page' => -1,
                'post_type' => 'product',
                'product_cat' => $product_cat,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id'
                    ),
                ),
                'orderby' => array('menu_order' => 'ASC', 'title' => 'ASC')
            );
            $loop = new WP_Query($args);
    

    魔术线是这样的:

    'orderby' => array('menu_order' => 'ASC', 'title' => 'ASC')
    
  • 0

    在尝试和搜索时,我找到了一个解决方案,使用两个查询来实现此目的,下面的示例可能会帮助您实现目的 .

    <?php

    $catargs = array(

    'orderby'                  => 'name',
    
    'order'                    => 'ASC',
    

    );

    $categories = get_categories( $catargs );

    foreach ($categories as $category) {?>

    <h3><?php echo $category->name; // Category title ?></h3> <?php

    // WP_Query arguments
    $args = array (
    
        'post_type'              => 'resources',
    
        'cat'                    => $category->cat_ID,
    
        'posts_per_page'         => '-1',
    
        'order'                  => 'ASC',
    
    'orderby'                => 'title',
    
    );
    
    // The Query
    $query = new WP_Query( $args );
    

    // The Loop

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            ?>
            <h5><?php the_title(); // Post title ?></h5>
            <?php 
            // You can all phone/ email here
        }
    } else {
        // no posts found
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    

    } ?>

    如果该方法适合您,请告诉我 .

相关问题