Wordpress WP_List_Table排序数据查询问题
Wordpress WP_List_Table sort data query problem
我使用的是 wordpress 5.5 和 php 7.4 以及 mysql 5.7.30
我正在为我的电子商务系统使用 woocomerce 订阅插件。
我想要的是能够在管理面板中显示 paginatd 订阅列表
并且(重要部分)根据管理面板中的“next_payment_schedule”、“first_payment_schedule”日期对它们进行排序。
woocommerce 订阅插件将所有订阅保存为自定义 post 类型,其类型为:“shop_subscription”,并且还将所有特定字段保存在 postmeta table.
正如我所说,问题是这个插件将这些日期保存在 post 元 table 中。
如何同时支持分页和排序?
这是我当前的查询:
SELECT p.id AS post_id, p.post_status, u.id AS user_id , u.user_login, u.user_email, GROUP_CONCAT(pm.meta_value)
FROM posts AS p
INNER JOIN users AS u ON p.post_author = u.id
INNER JOIN postmeta AS pm ON pm.post_id = p.id
WHERE post_type = 'shop_subscription'
AND pm.meta_key in ('_schedule_start', '_schedule_next_payment')
GROUP BY p.id
ORDER BY user_login ASC
当我想处理分页时,这个查询很好。
当我想处理排序时,下面的查询很好,
SELECT p.id AS post_id, p.post_title, p.post_status, p.post_name, u.id AS user_id , u.user_login, u.user_email, pm.meta_key, pm.meta_value from posts AS p
INNER JOIN postmeta AS pm ON pm.post_id = p.id
INNER JOIN users AS u ON p.post_author = u.id
WHERE post_type = 'shop_subscription'
AND pm.meta_key in ('_schedule_start', '_schedule_next_payment')
ORDER BY CASE WHEN pm.meta_key IN('_schedule_next_payment') THEN pm.meta_value END desc
我该如何处理这种情况?
最后我使用这个查询解决了我的问题:
$sql = "SELECT p.id AS post_id, p.post_status, u.id AS user_id , u.user_login, u.user_email, pm2.schedule_start, pm2.schedule_next_payment
FROM `$wpdb->posts` AS p
INNER JOIN `$wpdb->users` AS u ON p.post_author = u.id
INNER JOIN (
SELECT t.post_id,
MAX(t.schedule_start) AS schedule_start,
MAX(t.schedule_next_payment) AS schedule_next_payment
FROM (
SELECT
post_id,
case when pm.meta_key = '_schedule_start' then pm.meta_value else 0 end AS schedule_start,
case when pm.meta_key = '_schedule_next_payment' then pm.meta_value else 0 end AS schedule_next_payment
FROM (SELECT * FROM `$wpdb->postmeta` pm where pm.meta_key IN ('_schedule_next_payment', '_schedule_start')) AS pm
) AS t
GROUP BY t.post_id
)
AS pm2 ON pm2.post_id = p.id
WHERE post_type = '".self::POST_TYPE."'"
如果您仔细查看以下子查询:
SELECT t.post_id,
MAX(t.schedule_start) AS schedule_start,
MAX(t.schedule_next_payment) AS schedule_next_payment
FROM (
SELECT
post_id,
case when pm.meta_key = '_schedule_start' then pm.meta_value else 0 end AS schedule_start,
case when pm.meta_key = '_schedule_next_payment' then pm.meta_value else 0 end AS schedule_next_payment
FROM (SELECT * FROM `$wpdb->postmeta` pm where pm.meta_key IN ('_schedule_next_payment', '_schedule_start')) AS pm
) AS t
GROUP BY t.post_id
你可以看到我更改了 postmeta table 列和行(旋转 table)
我可以很容易地将它与我的帖子一起加入 table 并根据这两个日期字段进行排序。
我使用的是 wordpress 5.5 和 php 7.4 以及 mysql 5.7.30 我正在为我的电子商务系统使用 woocomerce 订阅插件。 我想要的是能够在管理面板中显示 paginatd 订阅列表 并且(重要部分)根据管理面板中的“next_payment_schedule”、“first_payment_schedule”日期对它们进行排序。 woocommerce 订阅插件将所有订阅保存为自定义 post 类型,其类型为:“shop_subscription”,并且还将所有特定字段保存在 postmeta table.
正如我所说,问题是这个插件将这些日期保存在 post 元 table 中。 如何同时支持分页和排序?
这是我当前的查询:
SELECT p.id AS post_id, p.post_status, u.id AS user_id , u.user_login, u.user_email, GROUP_CONCAT(pm.meta_value)
FROM posts AS p
INNER JOIN users AS u ON p.post_author = u.id
INNER JOIN postmeta AS pm ON pm.post_id = p.id
WHERE post_type = 'shop_subscription'
AND pm.meta_key in ('_schedule_start', '_schedule_next_payment')
GROUP BY p.id
ORDER BY user_login ASC
当我想处理分页时,这个查询很好。
当我想处理排序时,下面的查询很好,
SELECT p.id AS post_id, p.post_title, p.post_status, p.post_name, u.id AS user_id , u.user_login, u.user_email, pm.meta_key, pm.meta_value from posts AS p
INNER JOIN postmeta AS pm ON pm.post_id = p.id
INNER JOIN users AS u ON p.post_author = u.id
WHERE post_type = 'shop_subscription'
AND pm.meta_key in ('_schedule_start', '_schedule_next_payment')
ORDER BY CASE WHEN pm.meta_key IN('_schedule_next_payment') THEN pm.meta_value END desc
我该如何处理这种情况?
最后我使用这个查询解决了我的问题:
$sql = "SELECT p.id AS post_id, p.post_status, u.id AS user_id , u.user_login, u.user_email, pm2.schedule_start, pm2.schedule_next_payment
FROM `$wpdb->posts` AS p
INNER JOIN `$wpdb->users` AS u ON p.post_author = u.id
INNER JOIN (
SELECT t.post_id,
MAX(t.schedule_start) AS schedule_start,
MAX(t.schedule_next_payment) AS schedule_next_payment
FROM (
SELECT
post_id,
case when pm.meta_key = '_schedule_start' then pm.meta_value else 0 end AS schedule_start,
case when pm.meta_key = '_schedule_next_payment' then pm.meta_value else 0 end AS schedule_next_payment
FROM (SELECT * FROM `$wpdb->postmeta` pm where pm.meta_key IN ('_schedule_next_payment', '_schedule_start')) AS pm
) AS t
GROUP BY t.post_id
)
AS pm2 ON pm2.post_id = p.id
WHERE post_type = '".self::POST_TYPE."'"
如果您仔细查看以下子查询:
SELECT t.post_id,
MAX(t.schedule_start) AS schedule_start,
MAX(t.schedule_next_payment) AS schedule_next_payment
FROM (
SELECT
post_id,
case when pm.meta_key = '_schedule_start' then pm.meta_value else 0 end AS schedule_start,
case when pm.meta_key = '_schedule_next_payment' then pm.meta_value else 0 end AS schedule_next_payment
FROM (SELECT * FROM `$wpdb->postmeta` pm where pm.meta_key IN ('_schedule_next_payment', '_schedule_start')) AS pm
) AS t
GROUP BY t.post_id
你可以看到我更改了 postmeta table 列和行(旋转 table) 我可以很容易地将它与我的帖子一起加入 table 并根据这两个日期字段进行排序。