2017-12-29 16 views
2

In WooCommerce habe ich die folgende SQL-Abfrage, um Elternprodukte auszuwählen. Ich möchte nur das "Lager" Produkt auswählen.Erhalten Sie alle übergeordneten Produkte mit "instock" Lagerstatus in WooCommerce

Dies ist meine eigentliche SQL-Abfrage:

$query=$db->query("select * from wp_posts 
    where post_parent='0' and post_type='product' and post_status='publish' 
    group by ID 
    limit 10"); 

Wie kann ich auswählen nur "auf Lager" parent Produkte (aber nicht alle)? :

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

geprüft und Arbeiten

+0

Update Ihre Frage eine richtige Datenprobe und das erwartete Ergebnis – scaisEdge

+0

@scaisEdge wie gewünscht – kashalo

+0

aktualisiert hinzufügen sehe ich keine nennenswerte .. Probe Daten .. – scaisEdge

Antwort

1

Der richtige Weg nur Mutter Produkte mit einem Lager-Status wie "instock" zu bekommen.


Um nur die Produkt-IDs zu erhalten:

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>'; 
+1

danke genau das, was ich suche :) – kashalo

Verwandte Themen