Opened 6 years ago
Last modified 11 days ago
#50871 new defect (bug)
When exact is true and orderby set to relevance, there is a DB error on search results page
| Reported by: | 5um17 | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Query | Version: | |
| Severity: | normal | Keywords: | has-patch dev-feedback |
| Cc: | Focuses: |
Description
In search query, when exact is set to true and orderby set to relevance there is DB error
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DESC, wp_posts.post_date DESC LIMIT 0, 10' at line 1]
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE 'hello') OR (wp_posts.post_excerpt LIKE 'hello') OR (wp_posts.post_content LIKE 'hello'))) AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') ORDER BY DESC, wp_posts.post_date DESC LIMIT 0, 10
It is clear that both options together has no meaning. But it is compatibility issue between WP Extended Search and WooCommerce
WP Extended search has a feature to match exact sentence so it sets exact to true and later WooCommerce adds orderby => relevance causing this SQL error.
How to reproduce with just WP
- Add this code to theme or plugin
<?php add_action('pre_get_posts', function ( $query ){ $query->set( 'exact', true ); $query->set( 'orderby', 'relevance' ); $query->set( 'order', 'DESC' ); });
- Go to front-end and make a search, you will see the error.
Proposed fix
Here https://core.trac.wordpress.org/browser/tags/5.4.2/src/wp-includes/class-wp-query.php#L2357
We checking if ! empty( $q['search_orderby_title'] ) is not empty but we allow to call parse_search_order() when 'relevance' === $q['orderby'] causing ORDER BY DESC in SQL query without column name.
IMHO, we should not call parse_search_order() when search_orderby_title is empty regardless of orderby.
Attachments (1)
Change History (11)
#2
in reply to: ↑ description
@
6 years ago
#3
@
6 years ago
Thanks for checking @SergeyBiryukov
As far as I can tell, this is already the case in the current condition:
No actually this is not true.
There is no parenthesis in the current condition so it will results in
<?php var_dump( false && false || true );
Which will result in true.
In patch I am adding parenthesis
<?php var_dump( false && ( false || true ) );
Please correct me if I am wrong?
#4
@
6 years ago
As far as I can tell, there are parentheses in the current condition:
( false && ( false || true ) )
With the patch, that becomes:
( false && ( ( false || true ) )
but the logic is the same.
#5
@
6 years ago
Could you please take another look at https://core.trac.wordpress.org/browser/trunk/src/wp-includes/class-wp-query.php#L2368 ?
I am sorry but I don't see any braces wrapping last two condition.

I am showing the start and end of parentheses with arrow and highlighted internal braces. There is no braces wrapping last && and ||
And only because of this it is showing error.
#6
@
9 months ago
I can see database errors in the logs triggered by this issue (in 6.9.1) - I wrote the patch before finding this ticket, and it is as suggested.
To reproduce:
curl -s "http://localhost/?s=-test+-example&orderby=relevance"
produces log
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 'THEN 2 ELSE 6 END), wp_posts.post_date DESC
LIMIT 0, 10' at line 5 for query SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1 AND (((wp_posts.post_title NOT LIKE '%test%') AND (wp_posts.post_excerpt NOT LIKE '%test%') AND (wp_posts.post_content NOT LIKE '%test%')) AND ((wp_posts.post_title NOT LIKE '%example%') AND (wp_posts.post_excerpt NOT LIKE '%example%') AND (wp_posts.post_content NOT LIKE '%example%'))) AND (wp_posts.post_password = '') AND ((wp_posts.post_type = 'attachment' AND (wp_posts.post_status = 'publish')) OR (wp_posts.post_type = 'page' AND (wp_posts.post_status = 'publish')) OR (wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish')))
ORDER BY (CASE WHEN THEN 2 ELSE 6 END), wp_posts.post_date DESC
LIMIT 0, 10 made by require('wp-blog-header.php'), wp, WP->main, WP->query_posts, WP_Query->query, WP_Query->get_posts
suggested patch
diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php
index ba7395f9..65f013e9 100644
--- a/wp-includes/class-wp-query.php
+++ b/wp-includes/class-wp-query.php
@@ -2546,7 +2546,7 @@ class WP_Query {
// Order search results by relevance only when another "orderby" is not specified in the query.
if ( ! empty( $query_vars['s'] ) ) {
$search_orderby = '';
- if ( ! empty( $query_vars['search_orderby_title'] ) && ( empty( $query_vars['orderby'] ) && ! $this->is_feed ) || ( isset( $query_vars['orderby'] ) && 'relevance' === $query_vars['orderby'] ) ) {
+ if ( ! empty( $query_vars['search_orderby_title'] ) && ( ( empty( $query_vars['orderby'] ) && ! $this->is_feed ) || ( isset( $query_vars['orderby'] ) && 'relevance' === $query_vars['orderby'] ) ) ) {
$search_orderby = $this->parse_search_order( $query_vars );
}
#8
@
11 days ago
Still reproducible on 7.1.4 (clean latest.zip, md5 verified, no plugins, default config).
Unauthenticated requests:
/?s=-foo&orderby=relevance -> ORDER BY DESC, wp_posts.post_date DESC /?s=-foo+-bar&orderby=relevance -> ORDER BY (CASE WHEN THEN 2 ELSE 6 END), wp_posts.post_date DESC
Both produce a "WordPress database error ... SQL syntax" and the search silently returns no results (HTTP 200).
Root cause is operator precedence in WP_Query::get_posts():
if ( ! empty( $query_vars['search_orderby_title'] )
&& ( empty( $query_vars['orderby'] ) && ! $this->is_feed )
|| ( isset( $query_vars['orderby'] ) && 'relevance' === $query_vars['orderby'] ) ) {
This evaluates as (A && B) OR C. With orderby=relevance, C is true, so parse_search_order() runs even when search_orderby_title is empty (all terms are exclusions). Then implode() / reset() on the empty array build an empty WHEN or a bare DESC.
I checked whether this can be escalated to SQL injection: it cannot. Only non-excluded terms reach search_orderby_title, always via esc_like() + prepare(), and the broken fragment is built from constant strings. Impact is limited to a DB error and empty results.
The parenthesized condition from comment:6 fixes it (verified on 7.1.4, no DB errors after the patch):
if ( ! empty( $query_vars['search_orderby_title'] )
&& ( ( empty( $query_vars['orderby'] ) && ! $this->is_feed )
|| ( isset( $query_vars['orderby'] ) && 'relevance' === $query_vars['orderby'] ) ) ) {
This ticket was mentioned in PR #15129 on WordPress/wordpress-develop by erycson.
11 days ago
#9
When all search terms are exclusions (e.g. ?s=-foo&orderby=relevance),
search_orderby_title is empty, but missing parentheses in the condition in
WP_Query::get_posts() let orderby=relevance call parse_search_order()
anyway, producing ORDER BY DESC / CASE WHEN THEN 2 and a DB error.
This adds the parentheses so the relevance clause is only built when there
are title terms. Same fix as proposed in comment:6.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code, with code-intruder (my own static analysis tool)
Model(s): Claude Opus, Claude Haiku
Used for: The issue was surfaced by code-intruder (call-graph regression +
AI review of the flagged sinks). AI helped confirm the root cause, check that
it cannot be escalated to SQL injection, and verify the fix on a clean 7.1.4
install. I reviewed and tested the one-line ch
---
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.ange myself.
#10
@
11 days ago
PR with the parenthesized condition from comment:6: https://github.com/WordPress/wordpress-develop/pull/15129
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Thanks for the ticket!
As far as I can tell, this is already the case in the current condition:
if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) ) { $search_orderby = $this->parse_search_order( $q ); }$this->parse_search_order( $q )is only called if:$q['search_orderby_title']is not empty.$q['orderby']is empty and this is not a feed, OR$q['orderby']is set torelevance.If
$q['search_orderby_title']is empty, the first part of the condition is not satisfied, so::parse_search_order()is not called.So the analysis seems correct, but it seems like that patch won't work as expected.