Php 从商业中的相关产品中排除特定产品
在Woocommerce单一产品页面中,我试图排除任何以“I”开头的产品出现在相关产品中。我在Php 从商业中的相关产品中排除特定产品,php,sql,wordpress,woocommerce,product,Php,Sql,Wordpress,Woocommerce,Product,在Woocommerce单一产品页面中,我试图排除任何以“I”开头的产品出现在相关产品中。我在单个产品/related.php模板文件中尝试了以下操作: foreach($products as $product){ if (substr($product->post_title, 0, 1) === 'I') { unset($product); } } 但我还是错过了一些东西,因为它不起作用 如何从相关产品中排除标题以字母“I”开头的产品ID?这里
单个产品/related.php
模板文件中尝试了以下操作:
foreach($products as $product){
if (substr($product->post_title, 0, 1) === 'I') {
unset($product);
}
}
但我还是错过了一些东西,因为它不起作用
如何从相关产品中排除标题以字母
“I”
开头的产品ID?这里有一个小的自定义挂钩函数,它将从单个产品页面的相关产品中排除标题以字母“I”
开头的产品ID
我使用一个非常简单的SQL查询来排除这些产品ID
守则:
add_filter( 'woocommerce_related_products', 'exclude_ids_from_related_products', 10, 3 );
function exclude_ids_from_related_products( $related_posts, $product_id, $query_args ){
global $wpdb;
$starting_by = "I"; // Excluding product title starting with…
// Get all product IDs starting with "I" in an array to be excluded
$excluded_ids = $wpdb->get_col( "
SELECT ID FROM {$wpdb->prefix}posts WHERE post_type LIKE 'product'
AND post_status LIKE 'publish' AND post_title LIKE '$starting_by%'
" );
// Return an array of related product IDs without excluded product IDs
return array_diff ( $related_posts, $excluded_ids );
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作