WordPress.org

Make WordPress Core

Opened 22 months ago

Closed 9 months ago

Last modified 9 months ago

#21009 closed defect (bug) (invalid)

If name="post_type" is set in search template the archive template is loaded.

Reported by: ZaneMatthew Owned by:
Milestone: Priority: normal
Severity: minor Version: 3.4
Component: Posts, Post Types Keywords: reporter-feedback close
Focuses: Cc:

Description

I have a hidden form field named "post_type", which is used in the search form. When the user searches the archive.php template is loaded and not the search.php template.

I'm not sure if this is a bug, or unexpected behavior, note my fix was to use a different form field name.

Bug(?) url:

http://bmxraceevents.com/?s=maryland&post_type=events

Fixed url:

http://bmxraceevents.com/?s=maryland&type=events

http://zanematthew.com/blog/2012/06/unexpected-behavior-in-wordpress-custom-post-type-search-template/

Change History (14)

comment:1 SergeyBiryukov22 months ago

If that's a custom search form, why don't you rename the hidden field to type?

comment:2 ZaneMatthew22 months ago

I did, my fix is in the description. Just wanted to bring this to someones attention.

comment:3 nacin22 months ago

s= should always force a search template. Looks like a legitimate bug.

comment:4 SergeyBiryukov22 months ago

Ah, I see. Can't reproduce though.

  1. Created "book" post type using the basic example from Codex.
  2. Added a new book, "Around the World in Eighty Days".
  3. Tested /?s=around&type=book and /?s=around&post_type=book URLs with Twenty Eleven.
  4. Both resulting pages use the search template.
Last edited 22 months ago by SergeyBiryukov (previous) (diff)

comment:5 ericlewis22 months ago

I can't reproduce either following @SergeyBiryukov's steps and also creating a custom archive template for the 'book' CPT, which loaded the search template.

comment:6 ZaneMatthew22 months ago

Thanks for looking into it, guess its something buggy on my end.

comment:7 nacin22 months ago

  • Keywords reporter-feedback added

comment:8 WraithKenny19 months ago

I've duplicated this. I think is has to do with a conflict here:

...
'rewrite' => array('slug' => 'recipes'),
'query_var' => true,
'has_archive' => true,
...

where the archive rewrite trumps the search.

www.example.com/?post_type=recipes goes to the archive like it's supposed to, and www.example.com/?s=meat goes to search, but www.example.com/?s=meat&post_type=recipes loads up the archive.

It's probably the 'query_var' doing it right? I agree with Nacin about s trumping the 'query_var'.

comment:9 WraithKenny19 months ago

Nevermind, changing that had no effect and the example code on the codex included it. I was able to *reliably* replicate by adding and deleting the archive-recipes.php file tho; My theme doesn't have a generic archive.php file.

comment:10 WraithKenny19 months ago

again nevermind, 2011 also has an archive.php. I don't know why it's happening.

comment:11 WraithKenny19 months ago

anyway, the least I can offer is a symptom: that www.example.com/?s=meat&post_type=recipes gives me (via debug-bar)

Query Type: Search
Query Template: archive-recipes.php

and wp_title() gives me the Search page's title, while loading up the wrong template.

I'll keep looking to see if I can find the cause.

comment:12 WraithKenny19 months ago

found it. the most obvious cause: no search.php

comment:13 webord9 months ago

  • Keywords close added
  • Resolution set to invalid
  • Status changed from new to closed

If it's fixed then I'm closing the Ticket.

comment:14 helen9 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.