$ wpdb无法处理自定义(woocommerce)表

时间:2014-05-02 15:14:20

标签: php mysql wordpress woocommerce

我试图在此插件创建的自定义表格中检索由woocommerce保存的一些订单详细信息,我遇到了两个问题。

使用以下代码,即使我在页面上启用了错误报告,页面也会中断而没有可见的错误

global $wpdb;
$wpdb->woocommerce_order_items = $wpdb->prefix . 'woocommerce_order_items';
$wpdb->woocommerce_order_itemmeta = $wpdb->prefix . 'woocommerce_order_itemmeta';

$therapists = $wpdb->get_results("SELECT 
u.ID, 
m1.meta_value       as first_name,
m2.meta_value       as last_name,
m3.meta_value       as pma_skills,
m4.meta_value       as pma_photo,
m5.meta_value       as pma_job_titles,
m6.post_id          as order_id,
m7.order_item_id    as order_item_id,
m8.meta_value       as status,
m9.meta_value       as expiry_date,
m10.meta_value      as product_id

FROM $wpdb->users AS u

LEFT JOIN $wpdb->usermeta AS m1 
ON u.ID = m1.user_id  AND m1.meta_key = 'first_name'

LEFT JOIN $wpdb->usermeta AS m2 
ON u.ID = m2.user_id AND m2.meta_key = 'last_name'

LEFT JOIN $wpdb->usermeta AS m3
ON u.ID = m3.user_id AND m3.meta_key = 'pma_skills'

LEFT JOIN $wpdb->usermeta AS m4
ON u.ID = m4.user_id AND m4.meta_key = 'pma_photo'

LEFT JOIN $wpdb->usermeta AS m5
ON u.ID = m5.user_id AND m5.meta_key = 'pma_job_titles'

LEFT JOIN $wpdb->postmeta AS m6
ON u.ID = m6.meta_value

LEFT JOIN $wpdb->woocommerce_order_items AS m7
ON order_id = m7.order_id AND m7.order_item_type = 'line_item'

LEFT JOIN $wpdb->woocommerce_order_itemmeta AS m8
ON m7.order_item_id = m8.order_item_id AND m8.meta_key = '_subscription_status'

LEFT JOIN $wpdb->woocommerce_order_itemmeta AS m9
ON m7.order_item_id = m9.order_item_id AND m9.meta_key = '_subscription_expiry_date'

LEFT JOIN $wpdb->woocommerce_order_itemmeta AS m10
ON m7.order_item_id = m10.order_item_id AND m10.meta_key = '_product_id'

WHERE m1.meta_value IS NOT NULL");

使用以下代码代替自定义表从查询中删除

LEFT 加入 m7 ON order_id = m7.order_id AND m7.order_item_type =' line_item'

而不是

LEFT 加入wp_woocommerce_order_items AS m7 ON order_id = m7.order_id AND m7.order_item_type =' line_item'

global $wpdb;

$therapists = $wpdb->get_results("SELECT 
u.ID, 
m1.meta_value       as first_name,
m2.meta_value       as last_name,
m3.meta_value       as pma_skills,
m4.meta_value       as pma_photo,
m5.meta_value       as pma_job_titles,
m6.post_id          as order_id,
m7.order_item_id    as order_item_id,
m8.meta_value       as status,
m9.meta_value       as expiry_date,
m10.meta_value      as product_id

FROM $wpdb->users AS u

LEFT JOIN $wpdb->usermeta AS m1 
ON u.ID = m1.user_id  AND m1.meta_key = 'first_name'

LEFT JOIN $wpdb->usermeta AS m2 
ON u.ID = m2.user_id AND m2.meta_key = 'last_name'

LEFT JOIN $wpdb->usermeta AS m3
ON u.ID = m3.user_id AND m3.meta_key = 'pma_skills'

LEFT JOIN $wpdb->usermeta AS m4
ON u.ID = m4.user_id AND m4.meta_key = 'pma_photo'

LEFT JOIN $wpdb->usermeta AS m5
ON u.ID = m5.user_id AND m5.meta_key = 'pma_job_titles'

LEFT JOIN $wpdb->postmeta AS m6
ON u.ID = m6.meta_value

LEFT JOIN $wpdb->prefixwoocommerce_order_items AS m7
ON order_id = m7.order_id AND m7.order_item_type = 'line_item'

LEFT JOIN $wpdb->prefixwoocommerce_order_itemmeta AS m8
ON m7.order_item_id = m8.order_item_id AND m8.meta_key = '_subscription_status'

LEFT JOIN $wpdb->prefixwoocommerce_order_itemmeta AS m9
ON m7.order_item_id = m9.order_item_id AND m9.meta_key = '_subscription_expiry_date'

LEFT JOIN $wpdb->prefixwoocommerce_order_itemmeta AS m10
ON m7.order_item_id = m10.order_item_id AND m10.meta_key = '_product_id'

WHERE m1.meta_value IS NOT NULL");

如果我只使用完整的表名而不是变量,我也会得到一个没有错误的页面。

您可以在此页http://psicologialondra.com/testpage/

上查看结果

我做错了什么?我正在尝试使用推荐的代码,但它似乎不起作用:(

1 个答案:

答案 0 :(得分:0)

第二个代码无法正常工作,你绝对需要第一个代码。如果您使用该查询,是否打印出查询?我还会检查get_results方法是否使用 var_dump 返回任何错误。