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>
Hope this would help:
for more reference go to: Class Reference/WP Query - Custom Field Parameter
I Just did this and it work like a charm:-)