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

database - pre_get_posts causings DB error when using ( 'posts_per_page', -1)?

programmeradmin3浏览0评论

Has anyone encountered a DB error when using pre_get_posts to display ALL posts on a CPT archive page?

This error...

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1' at line 1]

SELECT SQL_CALC_FOUND_ROWS dfr34_wp_posts.ID FROM dfr34_wp_posts WHERE 1=1 AND dfr34_wp_posts.post_type = 'our-work' AND (dfr34_wp_posts.post_status = 'publish' OR dfr34_wp_posts.post_status = 'acf-disabled' OR dfr34_wp_posts.post_status = 'private') ORDER BY dfr34_wp_posts.post_date DESC LIMIT 0, -1

I'm using the following code and the error only shows if i try and show all posts, if i just put a number in it works.

 function work_query_order($query){
        if( ! is_admin() && is_post_type_archive( 'our-work') && $query->is_main_query()  ):
                    $query->set( 'posts_per_page', -1);         
        endif;    
};
add_action( 'pre_get_posts', 'work_query_order', 5 );
发布评论

评论列表(0)

  1. 暂无评论