Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user

Seach Search custom post type posts only by meta fields?

function __search_by_title_only( $search, &$wp_query )
{
    global $wpdb;
    if ( empty( $search ) )
        return $search; // skip processing - no search term in query
    $q = $wp_query->query_vars;
    $n = ! empty( $q['exact'] ) ? '' : '%';
    $search =
    $searchand = '';
    foreach ( (array) $q['search_terms'] as $term ) {
        $term = esc_sql( like_escape( $term ) );
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
     }
     if ( ! empty( $search ) ) {
         $search = " AND ({$search}) ";
         if ( ! is_user_logged_in() )
             $search .= " AND ($wpdb->posts.post_password = '') ";
          }
      return $search;
  }
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );
function __search_by_title_only($search, &$wp_query)
{
    global $wpdb;
    if (empty($search))
        return $search; // skip processing - no search term in query
    $q = $wp_query->query_vars;
    $n = !empty($q['exact']) ? '' : '%';
    $search =
        $searchand = '';
    foreach ((array) $q['search_terms'] as $term) {
        $term = esc_sql(like_escape($term));
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
    if (!empty($search)) {
        $search = " AND ({$search}) ";
        if (!is_user_logged_in())
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    return $search;
}
add_filter('posts_search', '__search_by_title_only', 500, 2);

Seach custom post type posts only by meta fields?

function __search_by_title_only( $search, &$wp_query )
{
    global $wpdb;
    if ( empty( $search ) )
        return $search; // skip processing - no search term in query
    $q = $wp_query->query_vars;
    $n = ! empty( $q['exact'] ) ? '' : '%';
    $search =
    $searchand = '';
    foreach ( (array) $q['search_terms'] as $term ) {
        $term = esc_sql( like_escape( $term ) );
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
     }
     if ( ! empty( $search ) ) {
         $search = " AND ({$search}) ";
         if ( ! is_user_logged_in() )
             $search .= " AND ($wpdb->posts.post_password = '') ";
          }
      return $search;
  }
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

Search custom post type posts only by meta fields?

function __search_by_title_only($search, &$wp_query)
{
    global $wpdb;
    if (empty($search))
        return $search; // skip processing - no search term in query
    $q = $wp_query->query_vars;
    $n = !empty($q['exact']) ? '' : '%';
    $search =
        $searchand = '';
    foreach ((array) $q['search_terms'] as $term) {
        $term = esc_sql(like_escape($term));
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
    if (!empty($search)) {
        $search = " AND ({$search}) ";
        if (!is_user_logged_in())
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    return $search;
}
add_filter('posts_search', '__search_by_title_only', 500, 2);
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Edited title to better reflect what is being asked as question is not only applicable to WooCommerce. Formatting and tag updates.
Source Link

WooCommerce product search Seach custom post type posts only by meta fields ( _sku )?

is it possibile to search product _skucustom post type product by _sku or other custom meta field with input text field? I don't want to use plugin, just need custom WP function.

I have found this solution for search only by title:

function __search_by_title_only( $search, &$wp_query )
{
    global $wpdb;
    if ( empty( $search ) )
        return $search; // skip processing - no search term in query
    $q = $wp_query->query_vars;
    $n = ! empty( $q['exact'] ) ? '' : '%';
    $search =
    $searchand = '';
    foreach ( (array) $q['search_terms'] as $term ) {
        $term = esc_sql( like_escape( $term ) );
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
     }
     if ( ! empty( $search ) ) {
         $search = " AND ({$search}) ";
         if ( ! is_user_logged_in() )
             $search .= " AND ($wpdb->posts.post_password = '') ";
          }
      return $search;
  }
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

... but I need function like this for meta fields ( _sku_sku and _author_author ).

Thanks !

WooCommerce product search only by meta fields ( _sku )?

is it possibile to search product _sku or other custom meta field with input text field? I don't want to use plugin, just need custom WP function.

I have found this solution for search only by title:

function __search_by_title_only( $search, &$wp_query )
{
    global $wpdb;
    if ( empty( $search ) )
        return $search; // skip processing - no search term in query
    $q = $wp_query->query_vars;
    $n = ! empty( $q['exact'] ) ? '' : '%';
    $search =
    $searchand = '';
    foreach ( (array) $q['search_terms'] as $term ) {
        $term = esc_sql( like_escape( $term ) );
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
     }
     if ( ! empty( $search ) ) {
         $search = " AND ({$search}) ";
         if ( ! is_user_logged_in() )
             $search .= " AND ($wpdb->posts.post_password = '') ";
          }
      return $search;
  }
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

... but I need function like this for meta fields ( _sku and _author ).

Thanks !

Seach custom post type posts only by meta fields?

is it possibile to search custom post type product by _sku or other custom meta field with input text field? I don't want to use plugin, just need custom WP function.

I have found this solution for search only by title:

function __search_by_title_only( $search, &$wp_query )
{
    global $wpdb;
    if ( empty( $search ) )
        return $search; // skip processing - no search term in query
    $q = $wp_query->query_vars;
    $n = ! empty( $q['exact'] ) ? '' : '%';
    $search =
    $searchand = '';
    foreach ( (array) $q['search_terms'] as $term ) {
        $term = esc_sql( like_escape( $term ) );
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
     }
     if ( ! empty( $search ) ) {
         $search = " AND ({$search}) ";
         if ( ! is_user_logged_in() )
             $search .= " AND ($wpdb->posts.post_password = '') ";
          }
      return $search;
  }
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

... but I need function like this for meta fields ( _sku and _author ).

Thanks !

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user