Woocommerce - List product by using specific attri

2019-09-01 02:47发布

I am trying to make a custom query to fetch product that have "League = NCAA" as showing in the picture. I have total 74 leagues of ncaa .

http://i.stack.imgur.com/MQDAL.png

Right now my query is like this but it shows nothing.

<ul class="products grid ncaa">
        <?php
    $args = array(
        'post_type' => 'product',
        'meta_key' => 'league',
        'meta_value' => 'ncaa',
        'posts_per_page' => 12

        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
         woocommerce_get_template( 'loop/no-products-found.php' );
    }

?>
      </ul>

After searching from the database for keyword 'NCAA' its shows me this table, but I don't know how to fetch from here:-

http://i.stack.imgur.com/kjK17.png

After viewing the table I tried to make this query and it shows me single product only at front end, but i want to display all ncaa attribute containing products.

 <ul class="products grid ncaa">
        <?php
    $args = array(
        'post_type' => 'product',
        'meta_key' => '_product_attributes',
        'meta_value' => 'a:2:{s:6:"league";a:6:{s:4:"name";s:6:"League";s:5:"value";s:4:"NCAA";s:11:"is_taxonomy";i:0;s:8:"position";i:0;s:10:"is_visible";i:1;s:12:"is_variation";i:0;}s:3:"upc";a:6:{s:4:"name";s:3:"UPC";s:5:"value";s:12:"889345125070";s:11:"is_taxonomy";i:0;s:8:"position";i:0;s:10:"is_visible";i:1;s:12:"is_variation";i:0;}}',
        'posts_per_page' => 12

        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
         woocommerce_get_template( 'loop/no-products-found.php' );
    }

?>
      </ul>

2条回答
成全新的幸福
2楼-- · 2019-09-01 03:25

Hope this would help:

 <ul class="products grid ncaa">
    <?php

$args = array(
    'meta_key'     => '_product_attributes',
    'meta_value'   => '',
    'meta_compare' => '!=',
    'post_type'    => 'product'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) : $loop->the_post();

        $data = get_post_meta($post->ID, '_product_attributes', true);
        if(in_array('NCAA',$data)){
            wc_get_template_part( 'content', 'product' );
        }
        else{
            //Do Nothing
        }
    endwhile;
} else {
     woocommerce_get_template( 'loop/no-products-found.php' );
}

?>
</ul>

for more reference go to: Class Reference/WP Query - Custom Field Parameter

查看更多
何必那么认真
3楼-- · 2019-09-01 03:49

I Just did this and it work like a charm:-)

 <ul class="products grid ncaa">
                            <?php
                            $args = array(
                                //'meta_compare' => 'LIKE',
                                'post_type'      => 'product',
                                'posts_per_page' => '74',
                                'meta_query'     => array(
                                    array(
                                        'key'     => '_product_attributes',
                                        'value'   => 'ncaa',
                                        'compare' => 'LIKE',
                                        'type'    => 'ncaa'
                                    )
                                )
                            );
                            $loop = new WP_Query( $args );

                            if ( $loop->have_posts() ) {
                                while ( $loop->have_posts() ) : $loop->the_post();
                                    {
                                        wc_get_template_part( 'content', 'product' );
                                    }
                                endwhile;
                            } else {
                                woocommerce_get_template( 'loop/no-products-found.php' );
                            }
                            ?>
                        </ul>
查看更多
登录 后发表回答