0

Here is my array which I am trying to get "apple" keyword in post table but it can't work like I want. Please let me know what is wrong with my array?

Array
(
    [paged] => 1
    [posts_per_page] => 50
    [tax_query] => Array
        (
            [0] => Array
                (
                    [taxonomy] => product_type
                    [field] => slug
                    [terms] => Array
                        (
                            [0] => chipin
                        )

                    [operator] => NOT IN
                )

        )

    [status] => publish
    [orderby] => date
    [order] => DESC
    [meta_query] => Array
        (
            [0] => Array
                (
                    [key] => _stock_status
                    [value] => instock
                )

            [1] => Array
                (
                    [key] => post_title
                    [value] => apple
                    [compare] => LIKE
                )

            [2] => Array
                (
                    [relation] => OR
                )

        )

    [title_filter] => apple
    [title_filter_relation] => OR
    [post_type] => product
)

Here is query

SELECT SQL_CALC_FOUND_ROWS  bhs_posts.ID
                    FROM bhs_posts  INNER JOIN bhs_postmeta ON ( bhs_posts.ID = bhs_postmeta.post_id )  INNER JOIN bhs_postmeta AS mt1 ON ( bhs_posts.ID = mt1.post_id )
                    WHERE 1=1  AND ( 
  bhs_posts.ID NOT IN (
                SELECT object_id
                FROM bhs_term_relationships
                WHERE term_taxonomy_id IN (918)
            )
) AND ( 
  ( bhs_postmeta.meta_key = '_stock_status' AND bhs_postmeta.meta_value = 'instock' ) 
  AND 
  ( mt1.meta_key = 'post_title' AND mt1.meta_value LIKE '{c914ad731b1921eeb5b858d7b618d23619ec92314bf850d67e343f6599dee9cc}apple{c914ad731b1921eeb5b858d7b618d23619ec92314bf850d67e343f6599dee9cc}' )
) AND ((bhs_posts.post_type = 'product' AND (bhs_posts.post_status = 'publish')))
                    GROUP BY bhs_posts.ID
                    ORDER BY bhs_posts.post_date DESC
                    LIMIT 0, 50

Database table:

enter image description here

If I can make query like following then get proper result but I don't know how to change array in Wp

SELECT SQL_CALC_FOUND_ROWS  bhs_posts.ID
                    FROM bhs_posts  INNER JOIN bhs_postmeta ON ( bhs_posts.ID = bhs_postmeta.post_id )  INNER JOIN bhs_postmeta AS mt1 ON ( bhs_posts.ID = mt1.post_id )
                    WHERE 1=1  AND ( 
  bhs_posts.ID NOT IN (
                SELECT object_id
                FROM bhs_term_relationships
                WHERE term_taxonomy_id IN (918)
            )
) AND ( 
  ( bhs_postmeta.meta_key = '_stock_status' AND bhs_postmeta.meta_value = 'instock' ) 
  AND 
  ( bhs_posts.post_title LIKE 'apple%' )
) AND ((bhs_posts.post_type = 'product' AND (bhs_posts.post_status = 'publish')))
                    GROUP BY bhs_posts.ID
                    ORDER BY bhs_posts.post_date DESC
                    LIMIT 0, 50
0

2 Answers 2

1

You added title inside $args['meta_query'] and that's why your query doesn't work. Checking the documentation (get_posts / WP_Query) you will find the available parameters, among them title and s.

$args = array(
    's'         => 'apple',
    'post_type' => 'product'
    //
    // other parameters
    // 'status`, 'orderby', 'meta_query', ...
)
$posts = get_posts($args);

If you use the title parameter - the exact match of the post title will be checked.
bhs_posts.post_title = 'some text'

If you use s parameter - (in simplification) the phrase will be splited and searched in the post title, content and excerpt .

((bhs_posts.post_title like '%some%' OR bhs_posts.post_excerpt like '%some%' OR bhs_posts.post_content like '%some%')
AND (bhs_posts.post_title like '%text%' OR bhs_posts.post_excerpt like '%text%' OR bhs_posts.post_content like '%text%'))

If you want to search only in the title of a post, you will need to use posts_search or posts_where filter to modify the query.

add_filter('posts_search', 'se414324_title_search', 100, 2)
function se414324_title_search( $search, $wp_query )
{
    global $wpdb;

    $qv = &$wp_query->query_vars;
    if ( !isset($qv['wpse_title_search']) || 0 == strlen($qv['wpse_title_search']) )
        return $search;

    $like = $wpdb->esc_like( $qv['wpse_title_search'] ) . '%';
    $search = $wpdb->prepare( " AND  {$wpdb->posts}.post_title LIKE %s", $like );
    return $search;
}

To modify only selected queries, and not all performed by WP, we use our own parameter name, here wpse_title_search in $args array.

$args = array(
    'wpse_title_search' => 'apple',
    'post_type' => 'product',
    //
    // other parameters
    // 'status`, 'orderby', 'meta_query', ...
)
$myquery = new WP_Query($args);

OR

// "suppress_filters" is necessary for the filters to be applied
$args = array(
    'wpse_title_search' => 'apple',
    'post_type' => 'product',
    'suppress_filters' => false,
    //
    // other parameters
    // 'status`, 'orderby', 'meta_query', ...
)
$posts = get_posts($args);
0
/* Use simple Query with s parameter */

$args = array(      
    'post_type'   => 'product',
    's'           => 'apple',
    'post_status' => 'publish',
    'posts_per_page' => -1,    
);

$wp_query = new WP_Query($args);
2
  • that will give too many results, posts which have the "word" in the content as well as the title Mar 3 at 8:20
  • 's' => 'apple' is also respond description field also I just need post_title only to get result. this is not working. I already tried. Mar 3 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.