Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#35930 closed defect (bug) (maybelater)

Search in meta posts

Reported by: sebastianpisula's profile sebastian.pisula Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Query Keywords:
Focuses: Cc:

Description

I try use this action to search custom field:

<?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.

Change History (4)

#1 @sebastian.pisula
8 years ago

  • Keywords needs-patch 2nd-opinion added

#2 @swissspidy
8 years ago

  • Keywords needs-patch 2nd-opinion removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

The default relation of a meta query defaults to AND, but you can change it to OR like this:

array(
        'relation' => 'OR', // Optional, defaults to "AND"
        array(
                'key'     => '_my_custom_key',
                'value'   => 'Value I am looking for',
                'compare' => '='
        )
);

See https://codex.wordpress.org/Class_Reference/WP_Meta_Query for more examples.

#3 @sebastian.pisula
8 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Relation in Meta Query is for few meta keys but not for relation between search fields (post title / excerpt / content) and meta query. Your solution not fixed this bug.

I think that my ticket this is bug.

Add custom post meta for post and use my action to test.

I want search in title or excerpt or content or custom post meta

Last edited 8 years ago by sebastian.pisula (previous) (diff)

#4 @dd32
8 years ago

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

Relation in Meta Query is for few meta keys but not for relation between search fields (post title / excerpt / content) and meta query

Correct, for WP_Query we currently only support the AND relation between parameters passed. You can't for example select post_id = 123 OR post_name = test-page, the same goes for search and meta_queries.

There's been similar proposals in the past to allow merging two WP_Query instances and returning a union of their results, for example: #4065

I'm going to close this as maybelater for now though, merging of two distinct queries or providing a way to specify the relation between individual top-level parameters isn't something we can easily support right now.

As for the original intention of wanting to search in a metakey as well, you'd be far better off using something custom or something based off one of the search-related SQL filters.

Note: See TracTickets for help on using tickets.