Created
June 10, 2015 18:54
-
-
Save thierrypigot/c3e15f1b9fc22bc16e9f to your computer and use it in GitHub Desktop.
Permet, en admin, de rechercher sur les postmeta, en plus du title
This file contains 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
//************** SEARCH FILTER **************// | |
add_filter('posts_join', 'revendeurs_search_join' ); | |
function revendeurs_search_join ($join){ | |
global $pagenow, $wpdb; | |
// I want the filter only when performing a search on edit page of Custom Post Type named "revendeurs" | |
if ( is_admin() && $pagenow=='edit.php' && $_GET['post_type']=='revendeurs' && $_GET['s'] != '') | |
{ | |
$join .='LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id '; | |
} | |
return $join; | |
} | |
add_filter( 'posts_where', 'revendeurs_search_where' ); | |
function revendeurs_search_where( $where ){ | |
global $pagenow, $wpdb; | |
// I want the filter only when performing a search on edit page of Custom Post Type named "revendeurs" | |
if ( is_admin() && $pagenow=='edit.php' && $_GET['post_type']=='revendeurs' && $_GET['s'] != '') | |
{ | |
$where = preg_replace( | |
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", | |
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where ); | |
} | |
return $where; | |
} | |
add_filter( 'posts_distinct', 'revendeurs_search_distinct' ); | |
function revendeurs_search_distinct( $where ){ | |
global $pagenow, $wpdb; | |
if ( is_admin() && $pagenow=='edit.php' && $_GET['post_type']=='revendeurs' && $_GET['s'] != '') | |
{ | |
return "DISTINCT"; | |
} | |
return $where; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment