如何通过数据库查询直接访问产品名称,产品价格和永久链接以及产品照片列

时间:2019-02-27 19:25:58

标签: sql wordpress woocommerce product

我如何通过数据库查询直接访问WordPress WooCommerce中的产品名称,产品价格,永久链接和产品的图像URL? 什么是SQL查询?

1 个答案:

答案 0 :(得分:0)

global $wpdb;

$metas = array(
        '_thumbnail_id', '_regular_price'
    );

    foreach ($metas as $i=>$meta_key) {
        $meta_fields[] = 'm' . $i . '.meta_value as ' . $meta_key;
        $meta_joins[] = ' left join ' . $wpdb->postmeta . ' as m' . $i . ' on m' . $i . '.post_id=' . $wpdb->posts . '.ID and m' . $i . '.meta_key="' . $meta_key . '"';
    }
    $request = "SELECT ID, post_title, " .  join(',', $meta_fields) . " FROM $wpdb->posts ";
    $request .=  join(' ', $meta_joins);
    $request .= " WHERE post_status='publish' AND post_type='product' AND $wpdb->posts.post_title like '%sample%'";



$product_details = $wpdb->get_row($request, OBJECT);


if(!empty($product_details)){

    $qry = "SELECT guid FROM $wpdb->posts WHERE ID=" . $product_details->_thumbnail_id;
    $uri_obj = $wpdb->get_row($qry, OBJECT);
    $product_details->_thumbnail_id = $uri_obj->guid;

}
echo '<pre>';print_r($product_details);exit;