1

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();
?>
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'
        )
    )
);
4
  • Hmm its display something,but i get some product from another category 'medalii' Nov 8 at 8:04
  • @DigitalPoint Could it be that those products are connected to multiple categories, that one of them is trofee? if thats the case you will need to exlude all other categories. Nov 8 at 8:09
  • The 3 product are in categories 'medalii' and 'medalii de metal' but they have nothing to do with the category 'trofee' Nov 8 at 8:44
  • The sort not working,it just display some products. For example if i put there 'medalii' display the same products Nov 8 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.