So you want only the “first” products having a unique product_cip? I think a direct MySQL Query may be quicker:
function get_all_product_in_selected_category($atts){
// Get the category
if(!(isset($atts['category_id']) || !is_numeric($atts['category_id'])){
return 0;
}
$catsearched = $atts['category_id'];
global $wpdb;
$productssql = "SELECT min($wpdb->posts.ID), meta_value
FROM $wpdb->posts
INNER JOIN $wpdb->term_relationships
ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy
ON ($wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id)
INNER JOIN $wpdb->postmeta
ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE
($wpdb->postmeta.meta_key = 'product_cip')
AND ($wpdb->posts.post_type="product")
AND ($wpdb->post_status="publish")
AND ($wpdb->term_taxonomy.taxonomy = 'product_cat')
AND ($wpdb->term_taxonomy.term_id = %d)
GROUP BY $wpdb->postmeta.meta_value";
$results = $wpdb->get_results($wpdb->prepare($productssql,$catsearched));
$a = array();
if($results){
foreach($results as $result){
$a[] = $result->ID;
}
return implode(',',$a);
}
return 0;
}
Obviously didn’t test this 😉