Make WordPress Core

Opened 14 years ago

Closed 12 years ago

Last modified 12 years ago

#13961 closed defect (bug) (fixed)

search query_vars bug when using search base

Reported by: shidouhikari's profile shidouhikari Owned by: markjaquith's profile markjaquith
Milestone: 3.5 Priority: normal
Severity: normal Version: 3.0
Component: Query Keywords: has-patch 3.5-early
Focuses: Cc:

Description

Use this debug code anywhere in a theme file:

global $wp_query;
$search_string = $wp_query->query_vars['s'];
echo "<p>$search_string</p>";

Then use a search form to do a normal search using more than a keyword, for exemple "search string", so that your testing site has posts that match it. It will result in something like http://domain.com/?s=search+string&submit=Go, debug will print "search string" and index.php will list all found posts as expected.

Now use search base version: http://domain.com/search/search+string ... debug prints "search+string" and since there's no post with "search-string", it is says there's no post matching.

I was unable to find where '+' should be but isn't replaced to ' ', and it's even more strange that only when search base is used that this bug happens.

Attachments (4)

13961.patch (508 bytes) - added by SergeyBiryukov 14 years ago.
13961.2.patch (508 bytes) - added by SergeyBiryukov 13 years ago.
13961.3.patch (527 bytes) - added by SergeyBiryukov 13 years ago.
13961.4.patch (571 bytes) - added by SergeyBiryukov 12 years ago.

Download all attachments as: .zip

Change History (36)

#1 @nacin
14 years ago

  • Milestone changed from 3.0.1 to Awaiting Review

#2 @nacin
14 years ago

  • Keywords reporter-feedback added

Use get_search_query() and the_search_query().

#3 @dd32
14 years ago

  • Component changed from General to Query
  • Keywords 3.2-early needs-patch added; reporter-feedback removed
  • Milestone changed from Awaiting Review to Future Release

The issue here is that ?s=test%20search results in get_search_query() returning 'test search', whilst, /search/test+search/ results in 'test+search' (as it should, as + is not the space character in this case), /search/test%20search/ results in 'test%20search' which IS wrong.

#4 @dd32
14 years ago

This also causes problems for non-ASCII language search terms: #16093

As originally reported by Vladimir Kolesnikov, search results for Cyrillic keywords are incorrect when using pretty permalinks.

When using ?s=%D1%81%D0%BB%D0%BE%D0%B2%D0%BE, the search string is decoded by PHP. When using search/%D1%81%D0%BB%D0%BE%D0%B2%D0%BE/, we should perform urldecode() ourselves.

#5 @dd32
14 years ago

The patch posted on 16093 does not work in some cases.

Patch is bad, It double-decodes ?s=... search requests, For example, ?s=test%2520search decodes to test search instead of test%20search.

It does however work for /search/test%2520search/ it searches for test%20search as correct.

Last edited 14 years ago by dd32 (previous) (diff)

#6 @SergeyBiryukov
14 years ago

  • Keywords has-patch added; needs-patch removed

#7 follow-up: @dd32
14 years ago

Do we need to stripslashes it when it's coming from a Permastruct?

We probably should also use $wp_rewrite->using_permastruct or something instead of empty($_GET)

#8 in reply to: ↑ 7 @SergeyBiryukov
14 years ago

Replying to dd32:

Do we need to stripslashes it when it's coming from a Permastruct?

Looks like not. However it doesn't seem to have a difference in this case.

Replying to dd32:

We probably should also use $wp_rewrite->using_permastruct or something instead of empty($_GET)

Did you mean $wp_rewrite->using_permalinks()? Since a search request can come through $_GET in this case too (and it does so in Twenty Ten search widget, regardless of the permalink structure), I guess empty($_GET) is a more reliable check.

#9 follow-up: @dd32
14 years ago

I guess empty($_GET) is a more reliable check.

You're right in that case, patch needs testing for stripslashes/etc but seems good for 3.2

#10 @SergeyBiryukov
14 years ago

  • Keywords needs-testing added

#11 @bennettmcelwee
14 years ago

  • Cc bennettmcelwee added

A note for testers. This patch should also fix a related issue to do with spaces. The URL

http://domain.com/search/search%20string

should carry out a search for "search string", but it currently searches for "search%20string" instead and thus fails to find any results.

For some live examples, try these:

The first two work and the third fails, but oddly enough the fourth one works... except that the displayed search string still has the plus sign in it.

#12 @bennettmcelwee
13 years ago

I have created a little plugin that effectively applies this patch.

http://wordpress.org/extend/plugins/search-fixer/

#13 in reply to: ↑ 9 @SergeyBiryukov
13 years ago

  • Keywords 3.2-early removed
  • Milestone changed from Future Release to 3.3

Replying to dd32:

You're right in that case, patch needs testing for stripslashes/etc but seems good for 3.2

Tested the patch with keywords like //test%20search and other examples from here.

Refreshed for 3.3.

#14 follow-up: @nacin
13 years ago

$_GET is a clever check but it seems like something could go wrong. It should probably only apply for is_main_query() in that case.

#15 @nacin
13 years ago

Also, I would think that + should be manually translated to a space for searches. That sounds like typical/proper behavior.

#16 in reply to: ↑ 14 @SergeyBiryukov
13 years ago

Replying to nacin:

It should probably only apply for is_main_query() in that case.

Done in 13961.3.patch.

#17 @azaozz
13 years ago

  • Milestone changed from 3.3 to Future Release

Not a regression or new bug.

#18 @SergeyBiryukov
13 years ago

  • Milestone changed from Future Release to 3.4

Closed #19694 as a duplicate.

#19 @sirzooro
13 years ago

  • Cc sirzooro added

#20 @nacin
12 years ago

  • Milestone changed from 3.4 to Future Release

#21 @nacin
12 years ago

  • Keywords 3.5-early added

#22 @nacin
12 years ago

Unit tests would be cool, if possible.

#23 @SergeyBiryukov
12 years ago

  • Keywords needs-unit-tests added; needs-testing removed

#24 @SergeyBiryukov
12 years ago

  • Milestone changed from Future Release to 3.5

Closed #21118 as a duplicate.

#25 @markjaquith
12 years ago

  • Owner set to markjaquith
  • Resolution set to fixed
  • Status changed from new to closed

In [21187]:

urldecode() search strings that come in from /search/foo. props SergeyBiryukov. fixes #13961

#26 @markjaquith
12 years ago

  • Keywords needs-unit-tests removed

Unit test: [UT755]

#27 follow-up: @nacin
12 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Should this be $this->is_main_query()? Or should we just always decode?

#28 in reply to: ↑ 27 ; follow-up: @markjaquith
12 years ago

Replying to nacin:

Should this be $this->is_main_query()? Or should we just always decode?

If someone is manually doing a new WP_Query, I don't think we want to decode.

#29 in reply to: ↑ 28 ; follow-up: @nacin
12 years ago

Replying to markjaquith:

Replying to nacin:

Should this be $this->is_main_query()? Or should we just always decode?

If someone is manually doing a new WP_Query, I don't think we want to decode.

Sure. But right now this will always fire on new WP_Query, unless query_posts() was used, as is_main_query() will just return true. This needs to be $this->is_main_query().

Kinda want to deprecate the is_main_query() function and leave just the method. It's confusing.

#30 @SergeyBiryukov
12 years ago

Just reviewed the process again. We only want to decode values that come from /search/, not from a custom query. $this->is_main_query() indeed makes more sense.

#31 @nacin
12 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In [21248]:

Use is_main_query() method, rather than the function. Only decode the search query variable if we are executing the main query and it came from the /search/ base, rather than the query string. fixes #13961.

#32 in reply to: ↑ 29 @SergeyBiryukov
12 years ago

Replying to nacin:

Kinda want to deprecate the is_main_query() function and leave just the method. It's confusing.

Follow-up: #23329

Note: See TracTickets for help on using tickets.