【发布时间】:2018-08-25 00:32:33
【问题描述】:
希望使用部分匹配来查找产品名称,例如,如果 product_name 是 cat-in-the-hat 并且 name 是 cat,那么 @ 987654325@ 将被退回。目前,需要完全匹配,即部分匹配是不可能的。
LIKE 语句通常可以工作,但我们使用IN 语句来支持不同数量的输入。根据MySQL LIKE IN()? 和REGEXP 也应该可以工作,但如前所述,输入的数量会有所不同。为每个 name 实例执行匹配的理想方法是什么?
function woo_products_by_name_shortcode( $atts, $content = null ) {
// Get attribuets
extract(shortcode_atts(array(
'name' => ''
), $atts));
if( empty($name) ) return;
// Format product slugs string
$name = str_replace(",", "','", $name);
global $wpdb;
// Get the corresponding products ids for each name
$ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->prefix}posts
WHERE post_type = 'product' AND post_name IN('$name')" );
ob_start();
// Define Query Arguments
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post__in' => $ids
));
// Get products number
$product_count = $loop->post_count;
echo '<pre>'; print_r($loop->posts); echo '</pre>';
return ob_get_clean();
}
add_shortcode("woo_products_by_name", "woo_products_by_name_shortcode");
【问题讨论】:
-
你考虑过只用
WHERE <condition> OR <condition> ...吗? -
这将对您有所帮助:stackoverflow.com/questions/907806/…
-
@fubar,是的,请查看链接的问题。就我而言,我不知道会有多少条件。 @anonyXmous 不是您引用的类似我已经在做的事情吗?问题是我需要为每个
IN匹配支持LIKE匹配。 -
这应该不是问题。你的条件会在一个数组中吗?