Created
January 20, 2014 19:10
-
-
Save freekrai/8527001 to your computer and use it in GitHub Desktop.
Quick Function: Search Custom Fields From wp-admin as per http://rogerstringer.com/2012/10/14/quick-function-search-custom-fields-from-wp-admin/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class post_search_meta { | |
| function post_search_meta() { | |
| add_filter( 'posts_where' , array( $this , 'where' ) ); | |
| add_filter( 'posts_join' , array( $this , 'join' ) ); | |
| add_filter( 'posts_groupby' , array( $this , 'group' ) ); | |
| } | |
| function group( $groupby ) { | |
| global $wp_query, $wpdb; | |
| if( empty($groupby) ) $groupby = "{$wpdb->posts}.ID"; | |
| if( !is_admin() || !$wp_query->query_vars['s'] ) return $groupby; | |
| $mygroupby = "{$wpdb->posts}.ID"; | |
| if( preg_match( "/$mygroupby/", $groupby )) { | |
| return $groupby; | |
| } | |
| if( !strlen(trim($groupby))) { | |
| return $mygroupby; | |
| } | |
| return $groupby . ", " . $mygroupby; | |
| } | |
| function where( $where ) { | |
| global $wp_query, $wpdb; | |
| if( !is_admin() || !$wp_query->query_vars['s'] ) return $where; | |
| $s = $wp_query->query_vars['s']; | |
| if( strpos( $where , 'AND (((' ) != false && strpos( substr( $where , 10 ) , 'AND (((' ) == false ) { | |
| $where = str_replace( 'AND (((' , "AND ((({$wpdb->postmeta}.meta_key LIKE '%$s%') OR ({$wpdb->postmeta}.meta_value LIKE '%$s%') OR (" , $where ); | |
| } | |
| return $where; | |
| } | |
| function join( $join ) { | |
| global $wp_query, $wpdb; | |
| if( is_admin() && isset( $wp_query->query_vars['s'] ) ) { | |
| $join .= " LEFT JOIN " . $wpdb->postmeta . " ON " . $wpdb->posts . ".ID = " . $wpdb->postmeta . ".post_id "; | |
| } | |
| return $join; | |
| } | |
| } | |
| $post_search_meta = new post_search_meta(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment