Make WordPress Core

Opened 10 years ago

Last modified 2 years ago

#27266 new defect (bug)

Front end search for attachment title cannot succeed

Reported by: dglingren's profile dglingren Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.9
Component: Query Keywords: has-patch has-unit-tests
Focuses: Cc:

Description (last modified by TobiasBg)

This is somewhat related to ticket #22556 but affects all queries that use the keyword search parameter, 's'.

Typing an attachment's title into the "front end" search box generated this query:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts 
WHERE 1=1 
AND (((wp_posts.post_title LIKE '%Guatemala%')
OR (wp_posts.post_content LIKE '%Guatemala%'))
AND ((wp_posts.post_title LIKE '%IRF3%')
OR (wp_posts.post_content LIKE '%IRF3%'))) 
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 (CASE
WHEN wp_posts.post_title LIKE '%Guatemala IRF3%'
THEN 1
WHEN wp_posts.post_title LIKE '%Guatemala%'
AND wp_posts.post_title LIKE '%IRF3%'
THEN 2
WHEN wp_posts.post_title LIKE '%Guatemala%'
OR wp_posts.post_title LIKE '%IRF3%'
THEN 3
WHEN wp_posts.post_content LIKE '%Guatemala IRF3%'
THEN 4
ELSE 5 END), wp_posts.post_date DESC
LIMIT 0, 2

Although the post_type clause includes attachment, the post_status test always fails because attachments have a post_status of inherit.

Attachments (1)

27266.diff (1.4 KB) - added by swissspidy 8 years ago.

Download all attachments as: .zip

Change History (18)

#1 @helen
10 years ago

  • Keywords close added

If you want your site search to include attachments, you'll need to alter the query via pre_get_posts or similar to include the inherit status. I'm not sure we should be changing this now, as it would cause behavior to change on people's sites whether they want it or not.

#2 @TobiasBg
10 years ago

  • Description modified (diff)
  • Keywords close removed

helen: I think, dglingren doesn't want to search for attachments, he's just saying that

AND wp_posts.post_type IN ('post', 'page', 'attachment')

is contradicting with

AND (wp_posts.post_status = 'publish'
OR wp_posts.post_author = 1
AND wp_posts.post_status = 'private') 

because attachments always have a post_status of inherit.

The front end search does indeed result in this SQL query. I could not yet check if attachments can have other post_status values.

The list of post types comes from get_post_types( array('exclude_from_search' => false) ).

#3 @dglingren
10 years ago

@TobiasBg interpreted my issue correctly; the two clauses contradict each other and I do not know whether the original intent was to include "Media Pages" in the search results or not. I am reporting this on behalf of one of my plugin users who does not understand why the front end search can't find any of the single attachment pages for his Media Library items, even when he puts the attachment title in the search box.

#4 @jeremyfelt
10 years ago

  • Version changed from trunk to 2.9

#5 follow-up: @dglingren
10 years ago

Re: version change

Did you really mean 2.9, or perhaps 3.9?

#6 in reply to: ↑ 5 @jeremyfelt
10 years ago

Replying to dglingren:

Did you really mean 2.9, or perhaps 3.9?

Version is used to indicate when it was first introduced. This has been around since post types were introduced (I think).

#7 @wonderboymusic
10 years ago

  • Keywords needs-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to Future Release

This seems like a valid concern. A patch could move this along nicely.

#8 @wonderboymusic
10 years ago

#25114 was marked as a duplicate.

#9 @chriscct7
8 years ago

  • Keywords early added

@swissspidy
8 years ago

#10 @swissspidy
8 years ago

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed

Since attachments now have pretty permalinks all the time (even without being actually attached to a post), it may make more sense to check for the 'inherit' post status here.

It would be quite a simple change, though people might wonder why their attachments are suddenly discoverable via search.

See 27266.diff

#11 @swissspidy
8 years ago

  • Keywords early removed
  • Milestone changed from Future Release to 4.6

#12 @swissspidy
8 years ago

  • Milestone changed from 4.6 to Future Release

#13 @swissspidy
8 years ago

  • Keywords 4.7-early added

This ticket was mentioned in Slack in #core-images by joemcgill. View the logs.


8 years ago

This ticket was mentioned in Slack in #core by helen. View the logs.


8 years ago

This ticket was mentioned in Slack in #core-images by joemcgill. View the logs.


8 years ago

#17 @johnbillion
2 years ago

  • Keywords 4.7-early removed
Note: See TracTickets for help on using tickets.