最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

query - Find woocommerce product ID by custom field value?

programmeradmin0浏览0评论

I try to find a way to get products ID by custom field in a category. I use this:

// 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)");
}

All works fine before FIND ID BY CIP After $arrayid is null.

I also tried:

    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;
}

But always bool(false)

Any idea ? Thanks

I try to find a way to get products ID by custom field in a category. I use this:

// 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)");
}

All works fine before FIND ID BY CIP After $arrayid is null.

I also tried:

    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;
}

But always bool(false)

Any idea ? Thanks

Share Improve this question asked Apr 14, 2018 at 18:15 ilanbilanb 933 silver badges12 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6
$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
}

Try This.

$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 to see multipal custom field condition.

This may help; this function returns a list of product where a custom field is set to any value.

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;
}

Works fine unless you've got 1000s of products.....

发布评论

评论列表(0)

  1. 暂无评论