[wp-trac] [WordPress Trac] #35930: Search in meta posts

WordPress Trac noreply at wordpress.org
Wed Feb 24 08:01:25 UTC 2016


#35930: Search in meta posts
------------------------------+-----------------------------
 Reporter:  sebastian.pisula  |      Owner:
     Type:  defect (bug)      |     Status:  new
 Priority:  normal            |  Milestone:  Awaiting Review
Component:  Query             |    Version:
 Severity:  normal            |   Keywords:
  Focuses:                    |
------------------------------+-----------------------------
 I try use this action to search custom field:

 {{{#!php
 <?php
 /**
  * Search by post meta
  *
  * @param WP_Query $query
  */
 function ic_post_meta_search( $query ) {
         if ( ! is_admin() && $query->is_main_query() &&
 $query->is_search() ) {

                 $query->set( 'meta_query', array(
                         array(
                                 'key'     => 'custom',
                                 'value'   => get_search_query(),
                                 'compare' => '=',
                         ),
                 ) );
         }
 }

 add_action( 'pre_get_posts', 'ic_post_meta_search' );
 }}}

 Problem is in WP_Query:


 {{{
 SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
 FROM wp_posts
 INNER JOIN wp_postmeta
 ON ( wp_posts.ID = wp_postmeta.post_id )
 WHERE 1=1
 AND (((wp_posts.post_title LIKE '%custom value%')
 OR (wp_posts.post_excerpt LIKE '%custom value%')
 OR (wp_posts.post_content LIKE '%custom value%')))
 AND ( ( wp_postmeta.meta_key = 'custom'
 AND CAST(wp_postmeta.meta_value AS CHAR) = 'custom value' ) )
 AND wp_posts.post_type IN ('post', 'page', 'attachment', 'project')
 AND (wp_posts.post_status = 'publish'
 OR wp_posts.post_status = 'acf-disabled'
 OR wp_posts.post_author = 1
 AND wp_posts.post_status = 'private')
 GROUP BY wp_posts.ID
 ORDER BY wp_posts.post_title LIKE '%custom value%' DESC,
 wp_posts.post_date DESC
 LIMIT 0, 5
 }}}

 Here is:

 {{{
  (((wp_posts.post_title LIKE '%custom value%') OR (wp_posts.post_excerpt
 LIKE '%custom value%') OR (wp_posts.post_content LIKE '%custom value%')))

 AND

 ( ( wp_postmeta.meta_key = 'custom' AND CAST(wp_postmeta.meta_value AS
 CHAR) = 'custom value' ) )
 }}}

 AND should be:

 Here is:

 {{{
  (((wp_posts.post_title LIKE '%custom value%') OR (wp_posts.post_excerpt
 LIKE '%custom value%') OR (wp_posts.post_content LIKE '%custom value%')))

 OR

 ( ( wp_postmeta.meta_key = 'custom' AND CAST(wp_postmeta.meta_value AS
 CHAR) = 'custom value' ) )
 }}}

 Chaange AND to OR fixed this problem.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/35930>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list