最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

SQL to Query the db and return all posts and it's metas

programmeradmin3浏览0评论

I am trying to query the wp db and return all posts and it's metas. However, the return is very slow. Am I doing it the right way to get the most optimal results?

Please see my SQL below:

SELECT CONCAT('<a href="',p.post_title,'">',p.post_title,'</a>') AS 'Projects Title', 
customer_username.meta_value AS 'wc_customer_username',
service_count.meta_value AS 'wc_service_count',
item_count.meta_value AS 'wc_item_count',
addon_count.meta_value AS 'wc_addon_count',
total.meta_value AS 'wc_total',
method_title.meta_value AS 'wc_method_title',
order_created.meta_value AS 'wc_order_created',
menu_order.meta_value AS 'Menu Order', 
levels.meta_value AS 'Levels'
FROM wp_posts AS p
JOIN wp_postmeta AS customer_username
  ON customer_username.post_id = p.ID 
JOIN wp_postmeta AS service_count
  ON service_count.post_id = p.ID 
JOIN wp_postmeta AS item_count
  ON item_count.post_id = p.ID 
JOIN wp_postmeta AS addon_count
  ON addon_count.post_id = p.ID 
JOIN wp_postmeta AS total
  ON total.post_id = p.ID 
JOIN wp_postmeta AS method_title
  ON method_title.post_id = p.ID
JOIN wp_postmeta AS order_created
  ON order_created.post_id = p.ID
JOIN wp_postmeta AS menu_order
  ON menu_order.post_id = p.ID
JOIN wp_postmeta AS levels
  ON levels.post_id = p.ID  
WHERE 1=1 
AND customer_username.meta_key = 'wc_customer_username'
AND service_count.meta_key = 'wc_service_count'
AND item_count.meta_key = 'wc_item_count'
AND addon_count.meta_key = 'wc_addon_count'
AND total.meta_key = 'wc_total'
AND method_title.meta_key = 'wc_method_title'
AND order_created.meta_key = 'wc_order_created'
AND menu_order.meta_key = 'wc_menu_order' 
AND levels.meta_key = 'wc_level' 
AND p.post_type = 'projects' AND levels.meta_value = '0'
ORDER BY menu_order.meta_key = 'wc_menu_order'
发布评论

评论列表(0)

  1. 暂无评论