如何使用POST_META触发连接查询

时间:2013-09-12 作者:Er.KT

我有在“posts\\u where”过滤器中运行的打印sql查询及其类似内容:

SELECT   wp_posts.* FROM wp_posts  WHERE 1=1  AND wp_posts.post_type = \'shop_order\' AND post_date >= \'2013-08-26\' AND post_date < \'2013-10-28\' AND post_date_gmt NOT LIKE \'0000%\'  ORDER BY wp_posts.post_date DESC 
现在,我想为post\\u meta值添加where条件,那么如何做到这一点呢?

此处包含post data和post\\u meta\\u数据的所有数据:

$metadata=get_post_meta($post->ID,\'order_data\');
echo "<pre>";print_r($metadata);exit;

Array
(
    [0] => Array
        (            
            [billing_first_name] => tetst
            [billing_last_name] => test
            [billing_company] => 
            [billing_address_1] => 1701,5120 25th St N
            [billing_address_2] => 
            [billing_city] => Irvington
            [billing_postcode] => 07111-4501
            [billing_country] => AF
            [billing_state] => NJ
            [billing_email] => [email protected]
            [billing_phone] => (973) 351-2932
            [shipping_first_name] => tetst
            [shipping_last_name] => test
            [shipping_company] => 
            [shipping_address_1] => 1701,5120 25th St N
            [shipping_address_2] => 
            [shipping_city] => Irvington
            [shipping_postcode] => 07111-4501
            [shipping_country] => AF
            [shipping_state] => NJ
            [shipping_method] => 
            [shipping_service] => 
            [payment_method] => paypal
            [payment_method_title] => PayPal
            [order_subtotal] => 10.00
            [order_discount_subtotal] => 40
            [order_shipping] => 30.00
            [order_discount] => 0.00
            [order_tax] => 
            [order_tax_no_shipping_tax] => 0
            [order_tax_divisor] => 100
            [order_shipping_tax] => 0.00
            [order_total] => 40.00            
            [order_tax_total] => 0.00
            [assignto] => 7
            [delivery_day_time] => 2013-09-12 01:30
        )

)
不在get\\u post\\u meta中传递密钥,其外观如下:

$metadata=get_post_meta($post->ID);
echo "<pre>";print_r($metadata);exit;

[order_data] => Array
        (
            [0] => a:38:{s:22:"order_discount_coupons";a:0:{}s:18:"billing_first_name";s:5:"tetst";s:17:"billing_last_name";s:4:"test";s:15:"billing_company";s:0:"";s:17:"billing_address_1";s:19:"1701,5120 25th St N";s:17:"billing_address_2";s:0:"";s:12:"billing_city";s:9:"Irvington";s:16:"billing_postcode";s:10:"07111-4501";s:15:"billing_country";s:2:"AF";s:13:"billing_state";s:2:"NJ";s:13:"billing_email";s:7:"[email protected]";s:13:"billing_phone";s:14:"(973) 351-2932";s:19:"shipping_first_name";s:5:"tetst";s:18:"shipping_last_name";s:4:"test";s:16:"shipping_company";s:0:"";s:18:"shipping_address_1";s:19:"1701,5120 25th St N";s:18:"shipping_address_2";s:0:"";s:13:"shipping_city";s:9:"Irvington";s:17:"shipping_postcode";s:10:"07111-4501";s:16:"shipping_country";s:2:"AF";s:14:"shipping_state";s:2:"NJ";s:15:"shipping_method";s:15:"";s:16:"shipping_service";s:15:"";s:14:"payment_method";s:6:"paypal";s:20:"payment_method_title";s:6:"PayPal";s:14:"order_subtotal";s:5:"10.00";s:23:"order_discount_subtotal";d:40;s:14:"order_shipping";s:5:"30.00";s:14:"order_discount";s:4:"0.00";s:9:"order_tax";s:74:"zero_rate:amount^0,rate^0,compound^,display^Tax,^0";s:25:"order_tax_no_shipping_tax";d:0;s:17:"order_tax_divisor";i:100;s:18:"order_shipping_tax";s:4:"0.00";s:11:"order_total";s:5:"40.00";s:39:"order_total_prices_per_tax_class_ex_tax";a:1:{s:18:"";d:10;}s:15:"order_tax_total";s:4:"0.00";s:8:"assignto";s:1:"7";s:17:"delivery_day_time";s:16:"2013-09-12 01:30";}
        )
现在,我想在“delivery\\u day\\u time”字段中应用where条件,

如何做到这一点?

UPDATED

现在,我获得的元数据如下:

[order_assign_data] => Array
        (
            [0] => 7
        )

    [order_assign_date] => Array
        (
            [0] => 2013-09-28 02:30
        )
现在如何应用条件?

1 个回复
SO网友:s_ha_dum

米洛告诉了你答案。您需要将数据存储为数据库中的单独存储,而不是作为序列化数组。MySQL无法以本机方式解析序列化数组,尽管我看到了非常复杂的SQL。尽管如此,该代码在生产服务器上仍然没有用处。这太复杂了,太慢了。所以

。。。本案是否有可能?

。。。如果您的意思是“我可以用我拥有的序列化数据来实现这一点吗?”然后是“不”,不是真的。您必须更改数据在数据库中的存储方式。如果您已经这样做了--也许这就是编辑的意思,我不太清楚--那么您需要的是JOIN.

SELECT 
  wp_posts.* 
FROM wp_posts 
LEFT JOIN wp_postmeta
  ON wp_postmeta.post_id = wp_posts.ID
WHERE 1=1  
AND wp_posts.post_type = \'shop_order\' 
AND post_date >= \'2013-08-26\' 
AND post_date < \'2013-10-28\' 
AND post_date_gmt NOT LIKE \'0000%\'  
AND wp_postmeta.meta_key = \'whatever\'
AND wp_postmeta.meta_value = \'somevalue\'
ORDER BY wp_posts.post_date DESC
连接的逻辑和SQL可能会变得复杂,因此您可能需要更多的代码。

您可能应该考虑使用WP_Query 使用meta_query 参数,而不是手动滚动的SQL。

至少使用$wpdb->posts$wpdb->postmeta 而不是硬编码的表名。你的东西不能随身携带。

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post