是否按自定义字段值查找WooCommerce产品ID?

时间:2018-04-14 作者:ilanb

我试图找到一种通过类别中的自定义字段获取产品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;
}
但总是布尔(错)

有什么想法吗?谢谢

3 个回复
SO网友:user173757
$args = array(
    \'post_type\' => \'product\',
    \'meta_key\' => \'YOUR_FIELD_ID\',
    \'meta_value\' => array(\'yes\'), //\'meta_value\' => array(\'yes\'),
    \'meta_compare\' => \'IN\' //\'meta_compare\' => \'NOT IN\'
);
$products = wc_get_products($args);

foreach ($products as $product) {
//do your logic
}
SO网友:Navnit Viradiya

试试这个。

$args = array(
    \'post_type\' => \'product\',
    \'post_status\' => \'publish\',
    \'posts_per_page\' => -1,
    \'meta_key\' => \'ingredients\', //Your Custom field name
    \'meta_value\' => \' \', //Custom field value
    \'meta_compare\' => \'=\'
);

$the_query = new WP_Query( $args );
$count = $the_query->post_count;
if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();
        $the_query->the_post();
        echo \'<li>\' . get_the_title() . \'</li>\'; 
    endwhile;
endif;
Click here 查看多路径自定义字段条件。

SO网友:Bret Weinraub

这可能会有所帮助;此函数用于返回自定义字段设置为任意值的产品列表。

function get_wc_products_where_custom_field_is_set($field) {
  $products = wc_get_products(array(\'status\' => \'publish\'));

  foreach ($products as $product) {
    $id = $product->get_id();
    if (get_post_meta($id,$field,true)) /* if my custom field is set */
      $ret[] = new \\WC_Product($id);
  }
  return $ret;
}
工作正常,除非你有1000种产品。。。。。

结束

相关推荐

自定义WP_QUERY循环中的AJAX无限滚动无法工作

我已经为页面模板设置了一个自定义WP\\U查询循环。我通过ajax实现了一个无限滚动方法,调用成功,但由于某些原因,我无法让查询喜欢循环中的分页参数。它什么也拉不动。以下是我的ajax操作代码:// AJAX Infinite Scroll function txcap_ajax_scroll() { $args = isset( $_POST[\'query\'] ) ? array_map( \'esc_attr\', $_POST[\'query\