posts_per_page设置为1,返回50个帖子

时间:2013-03-13 04:54:21

标签: mysql sql wordpress limit custom-post-type

我正在尝试以自定义帖子类型获取最新帖子:

$query = new WP_Query(array(
    'post_type'         => 'some_custom_post_type',
    'posts_per_page'    => 1,
    'orderby'           => 'date',
    'order'             => 'DESC'
));

出于某种奇怪的原因,这会返回50个帖子。我echo编辑了来自$query->request的SQL,我可以看到LIMIT设置为50:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
    AND wp_posts.post_type = 'some_custom_post_type'
    AND (
        wp_posts.post_status = 'publish'
        OR wp_posts.post_status = 'private'
    )
ORDER BY wp_posts.post_date DESC
LIMIT 0, 50

切换post_limits代替posts_per_page会产生同样奇怪的结果。


为什么会发生这种情况,我该如何解决?

1 个答案:

答案 0 :(得分:0)

$query = new WP_Query(array(
    'post_type'         => 'some_custom_post_type',
    'posts_per_page'    => 1,
    'showposts'         => 1,
    'orderby'           => 'date',
    'order'             => 'DESC'
));