php - How can I make this WordPress search optionally consider meta values? -


i'm trying configure wordpress search functionality consider data stored in meta field created advanced custom fields plugin.

i using code try , accomplish that:

if (!is_admin() && $query->is_search) {          $query->set('meta_query', array(             array(                 'key' => 'detailed_product_title',                 'value' => sanitize_text_field($_get['s']),                 'compare' => 'like'             )         )); } 

this yielding me search results of posts contain search term in meta field. example, if search "white", receive posts meta value contains white within them in search results.

this leads me question:

  • how can optionally search meta values, making search return posts hold search term somewhere within meta value, ignoring if meta value otherwise?

here current code:

function searchproducttitle($query) {     if (!is_admin() && $query->is_search) {          $query->set('meta_query', array(             'relation' => 'or',             array(                 'key' => 'detailed_product_title',                 'value' => sanitize_text_field($_get['s']),                 'compare' => 'like'             ),             array(                 'key' => 'detailed_product_title',                 'compare' => 'not exists'             )         ));      } } add_filter('pre_get_posts', 'searchproducttitle');  function my_posts_where($where) {      global $wpdb;      if ( isset( $_get['s'] ) && !empty( $_get['s'] ) ) {         $search = sanitize_text_field($_get['s']);          $where .= " or wp_posts.post_content '%' . $search . '%'";     }     return $where; } add_filter('posts_where' , 'my_posts_where'); 

try like:

    $query->set('meta_query', array(         'relation' => 'or',         array(             'key' => 'detailed_product_title',             'value' => sanitize_text_field($_get['s']),             'compare' => 'like'         ),         array(             'key' => 'detailed_product_title',             'compare' => 'not exists'         )     )); 

update:

you won't able directly combine meta_query above searching post_content field (using , or relation). can instead of above, add custom query this:

add_filter( 'posts_where', 'my_posts_where' );  function my_posts_where( $where ) {     if ( is_search() ) {          global $wpdb;         $search = get_search_query();         $search = like_escape( $search );           // include postmeta in search         $where .= " or {$wpdb->posts}.id in (select {$wpdb->postmeta}.post_id {$wpdb->posts}, {$wpdb->postmeta} {$wpdb->postmeta}.meta_key = 'detailed_product_title' , {$wpdb->postmeta}.meta_value '%$search%' , {$wpdb->posts}.id = {$wpdb->postmeta}.post_id)";      }      return $where; } 

note haven't tested above, give general idea of need do.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -