admin管理员组

文章数量:1278826

I don't know why i can't take the products by category name. I get no product found. The code look like this

 <ul class="glide__slides">
<?php
    $args = array(
        'post_type'      => 'product',
        'category_name' => 'trofee',
        'posts_per_page' => 20,
        'meta_query'     => array(
                'relation' => 'OR',
                array( // Simple products type
                    'key'           => '_sale_price',
                    'value'         => 0,
                    'compare'       => '>',
                    'type'          => 'numeric'
                ),
                array( // Variable products type
                    'key'           => '_min_variation_sale_price',
                    'value'         => 0,
                    'compare'       => '>',
                    'type'          => 'numeric'
                )
            )
    );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
        global $product;
        $id = $product->get_id();
        echo '<li class="glide__slide">';
        echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' <br> '.get_the_title().   '<br>' . '<div class="bf-sale"><span>' . $product->get_regular_price() .' Lei </span>' . '<span>' . $product->get_sale_price()  .'Lei</span></div>' .'</a>';
        echo '<div class="trophies-container__trophy-action">';
        echo sprintf(' <a href="?add-to-cart=%d data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="%d" data-product_sku=""  rel="nofollow"><i class="fas fa-shopping-cart"></i>Adaugă în coș</a>',$id,$id);
        echo '</div>';
        echo '</li>';
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();
?>

I don't know why i can't take the products by category name. I get no product found. The code look like this

 <ul class="glide__slides">
<?php
    $args = array(
        'post_type'      => 'product',
        'category_name' => 'trofee',
        'posts_per_page' => 20,
        'meta_query'     => array(
                'relation' => 'OR',
                array( // Simple products type
                    'key'           => '_sale_price',
                    'value'         => 0,
                    'compare'       => '>',
                    'type'          => 'numeric'
                ),
                array( // Variable products type
                    'key'           => '_min_variation_sale_price',
                    'value'         => 0,
                    'compare'       => '>',
                    'type'          => 'numeric'
                )
            )
    );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
        global $product;
        $id = $product->get_id();
        echo '<li class="glide__slide">';
        echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' <br> '.get_the_title().   '<br>' . '<div class="bf-sale"><span>' . $product->get_regular_price() .' Lei </span>' . '<span>' . $product->get_sale_price()  .'Lei</span></div>' .'</a>';
        echo '<div class="trophies-container__trophy-action">';
        echo sprintf(' <a href="?add-to-cart=%d data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="%d" data-product_sku=""  rel="nofollow"><i class="fas fa-shopping-cart"></i>Adaugă în coș</a>',$id,$id);
        echo '</div>';
        echo '</li>';
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();
?>
Share Improve this question asked Nov 8, 2021 at 7:51 Digital PointDigital Point 153 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

category_name is for category taxonomy, sense products use the product_cat you will need to add a tax_query into $args.
Like this.

$args = array(
    'post_type'      => 'product',
    'posts_per_page' => 20,
    'tax_query' => [
        'taxonomy' => 'product_cat',
        'field'    => 'name',
        'terms'    => 'trofee',
    ],
    'meta_query'     => array(
        'relation' => 'OR',
        array( // Simple products type
            'key'           => '_sale_price',
            'value'         => 0,
            'compare'       => '>',
            'type'          => 'numeric'
        ),
        array( // Variable products type
            'key'           => '_min_variation_sale_price',
            'value'         => 0,
            'compare'       => '>',
            'type'          => 'numeric'
        )
    )
);

本文标签: wp queryTake all produts by category