Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Search WordPress with custom field

function me_search_query( $query ) {
  if ( $query->is_search ) {
    $meta_query_args = array(
      array(
        'key' => 'your_key',
        'value' => $query->query_vars['s'],
        'compare' => 'LIKE',
      ),
    );
    $query->set('meta_query', $meta_query_args);
    add_filter( 'get_meta_sql', 'me_replace_and_with_or' );
  };
}

function me_replace_and_with_or( $sql ) {
    if ( 1 === strpos( $sql['where'], 'AND' ) ) {
        $sql['where'] = substr( $sql['where'], 4 );
        $sql['where'] = ' OR ' . $sql['where'];
    }

    //make sure that this filter will fire only once for the meta query
    remove_filter( 'get_meta_sql', 'me_replace_and_with_or' );
    return $sql;
}

add_filter( 'pre_get_posts', 'me_search_query');
Comment

search field wordpress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * Generate custom search form
 *
 * @param string $form Form HTML.
 * @return string Modified form HTML.
 */
function wpdocs_my_search_form( $form ) {
    $form = '<form role="search" method="get" id="searchform" class="searchform" action="' . home_url( '/' ) . '" >
    <div><label class="screen-reader-text" for="s">' . __( 'Search for:' ) . '</label>
    <input type="text" value="' . get_search_query() . '" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search' ) .'" />
    </div>
    </form>';
 
    return $form;
}
add_filter( 'get_search_form', 'wpdocs_my_search_form' );
Comment

wordpress search in custom fields

If someone is looking for a solution to this problem is to recommend the plugin:
https://wordpress.org/plugins/acf-better-search/
Comment

Search by custom field in wordpress

function cf_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    }

    return $join;
}
add_filter('posts_join', 'cf_search_join' );
function cf_search_where( $where ) {
    global $pagenow, $wpdb;

    if ( is_search() ) {
        $where = preg_replace(
            "/(s*".$wpdb->posts.".post_titles+LIKEs*('[^']+')s*)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

function cf_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );
Comment

wordpress search in custom fields

So, I did another search today and this was worked first time. http://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/
Comment

PREVIOUS NEXT
Code Example
Php :: php datetime create 
Php :: get post thumbnail url 
Php :: convert stdclass to json in php 
Php :: header cross origin using php only for our domains and subdomain 
Php :: Search for text in string php 
Php :: only alphanumeric characters are allowed regex in php 
Php :: php validate name 
Php :: get logged user id laravel 
Php :: wp override home url with php 
Php :: php date format 
Php :: get current route laravel 
Php :: function that checks number only in php 
Php :: How to display data from MySQL database into HTML table using PHP 
Php :: missing view cakephp 
Php :: acf get sub field 
Php :: array_intersect php 
Php :: PHP strlen — Get string length 
Php :: php get username from iis 
Php :: Warning: mysqli_error() expects exactly 1 parameter, 0 
Php :: wordpress get category page title 
Php :: php find lowest value in associative array 
Php :: write file in php 
Php :: centos search directory php.exe 
Php :: php carbon convert string to date 
Php :: exclude methods in resource route laravel 
Php :: php is day light saving time 
Php :: how to take input in php 
Php :: how to remove public from url in laravel 
Php :: pdo php check if row exist 
Php :: minuscule string php 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =