我正在尝试进行自定义查询以获取具有“League = NCAA”的产品,如图所示。我总共有74个 ncaa联赛。
http://i.stack.imgur.com/MQDAL.png
现在我的查询是这样的,但它没有显示任何内容。
<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>
从数据库中搜索关键字'NCAA'后,它会向我显示此表,但我不知道如何从这里获取: -
http://i.stack.imgur.com/kjK17.png
查看表格后,我尝试进行此查询,它只显示前端的单个产品,但我想显示所有包含产品的 ncaa 属性。
<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>
答案 0 :(得分:1)
我刚刚做了这个,它就像一个魅力:-)
<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>
答案 1 :(得分:0)
希望这会有所帮助:
<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>
如需更多参考,请访问:Class Reference/WP Query - Custom Field Parameter