The right way to get only parent products with a stock status like "instock":
:
global $wpdb;
// The SQL query
$results = $wpdb->get_results( "
SELECT p.*, pm.meta_value as stock_status
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'product'
AND p.post_status LIKE 'publish'
AND p.post_parent = '0'
AND pm.meta_key LIKE '_stock_status'
AND pm.meta_value LIKE 'instock'
GROUP BY p.ID
" );
// Testing output (objects array)
echo '<pre>'; print_r($results); echo '</pre>';
Tested and works.
To get only the product IDs:
global $wpdb;
// The SQL query
$results = $wpdb->get_col( "
SELECT p.ID
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'product'
AND p.post_status LIKE 'publish'
AND p.post_parent = '0'
AND pm.meta_key LIKE '_stock_status'
AND pm.meta_value LIKE 'instock'
" );
// Testing output (array of IDs)
echo '<pre>'; print_r($results); echo '</pre>';