感谢您的回答,我终于成功地通过自定义SQL查询实现了我想要的功能:)
我是如何做到这一点的:首先,我通过添加以下代码获得了使用WP\\u query生成的SQL查询:
$query = new WP_Query( $query_args );
//write the SQL query
echo $query->request;
我得到的是:
SELECT wp_posts.ID
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type = \'work\'
AND (wp_posts.post_status = \'publish\')
ORDER BY wp_posts.post_date DESC
它返回按最新优先顺序排列的帖子的完整列表。然后我添加了
offset
只需声明一个偏移量变量并在每一行中递增,如下所示:
SET @offset=0;
SELECT wp_posts.ID,
@offset := @offset + 1 AS offset
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type = \'work\'
AND (wp_posts.post_status = \'publish\')
ORDER BY wp_posts.post_date DESC
最后一步,我按post ID过滤了这个ID+偏移量关联列表,并添加了自定义行(
page
和
position
) :
SET @offset=0;
SELECT ID,
offset - 1 AS offset,
CEIL(offset / 10) AS page,
(offset - 1) % 10 AS position
FROM (SELECT wp_posts.ID,
@offset := @offset + 1 AS offset
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type = \'work\'
AND (wp_posts.post_status = \'publish\')
ORDER BY wp_posts.post_date DESC)
AS OFFSETS
WHERE ID = 83
(其中83是搜索的帖子ID,10是每页的帖子数量)
我用过offset - 1 AS offset
从0获取偏移,但可以使用offset
从1获取偏移。
因此,这将返回以下表格:
id=83,每页帖子=10:
ID | offset | page | position
83 | 16 | 2 | 6
->第2页第6位
id=10,每页帖子数=5:
ID | offset | page | position
10 | 33 | 7 | 3
->第7页第3位
我希望这会对某人有所帮助!:)