Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#25629 closed defect (bug) (worksforme)

Taxonomy get_sql() generate incorrect query

Reported by: xavismeh's profile xavismeh Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5.1
Component: Taxonomy Keywords: needs-unit-tests reporter-feedback
Focuses: Cc:

Description

Hello,

I'm using Wordpress 3.5.1 and came to an incorrectly generated query when trying to display the pages list causing no display of any page. WP is configured to run multiple sites and I'm using the administrator account. Here is the incorrect generated query :

SELECT   prefix_posts.* FROM prefix_posts  WHERE 1=1  AND 0 = 1 AND prefix_posts.post_type = 'page' AND (prefix_posts.post_status = 'publish' OR prefix_posts.post_status = 'future' OR prefix_posts.post_status = 'draft' OR prefix_posts.post_status = 'pending' OR prefix_posts.post_status = 'private') GROUP BY prefix_posts.ID ORDER BY prefix_posts.menu_order,prefix_posts.post_title asc

As you can see, the "WHERE 0=1" cant' return anything. I bypassed this error using the diff below. Note that as of today, the error still exists in the svn trunk.

Index: wp-includes/taxonomy.php
===================================================================
--- wp-includes/taxonomy.php	(revision 148746)
+++ wp-includes/taxonomy.php	(working copy)
@@ -678,11 +678,27 @@
 
 				if ( empty( $terms ) ) {
 					if ( 'OR' == $this->relation ) {
-						if ( ( $index + 1 === $count ) && empty( $where ) )
-							return self::$no_results;
+						if ( ( $index + 1 === $count ) && empty( $where ) ) {
+                            if (!empty($where)) {
+                                $where = ' AND (' . implode(" $this->relation ", $where) . ')';
+                            }
+                            else {
+                                $where = '';
+                            }
+
+                            return array('join' => $join, 'where' => $where);
+                        }
+
 						continue;
 					} else {
-						return self::$no_results;
+                        if (!empty($where)) {
+                            $where = ' AND (' . implode(" $this->relation ", $where) . ')';
+                        }
+                        else {
+                            $where = '';
+                        }
+
+                        return array('join' => $join, 'where' => $where);
 					}
 				}
 

Attachments (1)

fix-core.patch (1.2 KB) - added by xavismeh 11 years ago.

Download all attachments as: .zip

Change History (6)

@xavismeh
11 years ago

#1 @markoheijnen
11 years ago

Can you also check this in trunk. 3.5.1 is already a few versions old

#2 @xavismeh
11 years ago

It's written in my report : the bug still exists in the trunk ;)

#3 @markoheijnen
11 years ago

  • Keywords needs-unit-tests added

I guess I read the fast. Can you provide the code how you ended up with that query? This is something that needs some unit tests.

#4 @nacin
11 years ago

  • Keywords reporter-feedback added; has-patch dev-feedback needs-testing removed
  • Severity changed from critical to normal

There's no evidence there is a bug here.

0 = 1 is deliberately added when the query should not return any results. That's what self:$no_results contains. For whatever reason, WordPress has determined that the taxonomy query you are running should not return any results. It is possible that the bug is on your end.

It's certainly possible that your query parameters are valid, and should return results. But you'll need to provide more information, like what you are passing to WP_Query, whether you can reproduce this without plugins enabled, etc.

#5 @wonderboymusic
11 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

xavismeh, feel free to reopen if you have valid parameters that are producing an invalid query, but there has been no response in 6 months.

Note: See TracTickets for help on using tickets.