我试图找到一种通过类别中的自定义字段获取产品ID的方法。我用这个:
// Get the category
$catsearched = $atts[\'category_id\'];
// Get all product of categoryselected
$product_args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'fields\' => \'ids\', // Only return product IDs
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'id\',
\'terms\' => $catsearched,
\'operator\' => \'IN\',
))
);
$products = get_posts($product_args);
//$products = implode(",", $products);
//return $products;
/**
* FIND ALL PRODUCT WITH SAME CIP AND KEEP ONLY ONE PRODUCT BY CIP (array_unique)
*
* ======================================================================================
*/
foreach ( $products as $id )
{
$cip = $product_obj[\'product_cip\'] = get_post_meta($id,\'product_cip\');
$arrayCip[] = $cip[0];
}
//echo \'<b>TotalNumberOfCIP = \'.count($arrayCip).\'</b><br>\';
$result = array_unique($arrayCip);
//echo \'<b>TotalNumberOfUniqueCIP = \'.count($result).\'</b><br>\';
//$results = implode(",", $result);
//var_dump($results);
/**
* FIND ID BY CIP
*
**/
global $wpdb;
// Find product id by CIP
foreach($result as $cip)
{
$arrayid[] = $wpdb->get_var( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key=\'product_cip\' AND meta_value=\'.$cip.\' AND {$wpdb->term_relationships}.term_taxonomy_id IN ($catsearched)");
}
在$arrayid为null后,CIP查找ID之前,所有工作正常。
我还尝试了:
global $wpdb;
$meta = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s AND {$wpdb->term_relationships}.term_taxonomy_id IN ($catsearched)", $key, $value ) );
//var_dump(($meta));
if (is_array($meta) && !empty($meta) && isset($meta[0])) {
$meta = $meta[0];
}
if (is_object($meta)) {
return $meta->post_id;
}
else {
return false;
}
但总是布尔(错)
有什么想法吗?谢谢