Is "SELECT TOP 0" a bug with MS SQL database?

I'm having an issue that my posts page displays "no posts found" while right above it, there is "All (1) | Published (1)" (it is a new installation). The post is also invisible on the live site, not only in the administration.

I guess it might have something to do with my database which is SQL Server using WP Db Abstraction. So I installed debug bar and found this query:

SELECT TOP 0 * FROM wp_posts WHERE 1=1 AND wp_posts.post_type LIKE 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status LIKE 'future' OR wp_posts.post_status LIKE 'draft' OR wp_posts.post_status LIKE 'pending' OR wp_posts.post_status LIKE 'private') ORDER BY wp_posts.post_date DESC

WP_Posts_List_Table-prepare_items, wp_edit_posts_query, wp, WP-main, WP-query_posts, WP_Query-query, WP_Query-get_posts, pdo_wpdb-query, pdo_wpdb-_post_query 

SELECT TOP 0 looks suspicious to me, what would you say? If that's indeed a bug, any suggestion on how to fix it?

Topic mssql troubleshooting Wordpress

Category Web


The problem is not with the "select top 0", but the way the query is changed behind the scenes. Even if you change it to "select * from...", it will break.

I managed to get this working by going to "wp-includes\query.php" file, and then removing the $limits variable from the query. This is not the best way to fix the problem, but will get the site working. It looks like the $limits is breaking the query, and the 'row offset' of SQL server is different to MySql. I don't know to much about how it all works, but I managed to get my installation working.

In the query.php file...

Find this line:

$this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";

change to:

$this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby";

This solution will only work while you still use the your current version of WP.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.