Last active
June 30, 2016 19:21
-
-
Save romuloctba/636300ad087813112a13155b89908221 to your computer and use it in GitHub Desktop.
Wp-Admin search by meta value
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 | |
/** | |
* Plugin Name: Search By Meta | |
* Plugin URI: http://wordpress.stackexchange.com/questions/11758/extending-the-search-context-in-the-admin-list-post-screen | |
* Description: SearchByMeta. | |
* Author: SearchByMeta | |
* Author URI: http://wordpress.stackexchange.com/questions/11758/extending-the-search-context-in-the-admin-list-post-screen | |
**/ | |
add_filter('posts_join', 'catalog_product_search_join' ); | |
function catalog_product_search_join ($join){ | |
global $pagenow, $wpdb; | |
// I want the filter only when performing a search on edit page of Custom Post Type named "catalog_product" | |
if ( is_admin() && $pagenow=='edit.php' && $_GET['post_type']=='catalog_product' && $_GET['s'] != '') { | |
$join .='LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id '; | |
} | |
return $join; | |
} | |
add_filter( 'posts_where', 'catalog_product_search_where' ); | |
function catalog_product_search_where( $where ){ | |
global $pagenow, $wpdb; | |
// I want the filter only when performing a search on edit page of Custom Post Type named "catalog_product" | |
if ( is_admin() && $pagenow=='edit.php' && $_GET['post_type']=='catalog_product' && $_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; | |
} | |
function catalog_product_search_distinct( $where ){ | |
global $pagenow, $wpdb; | |
if ( is_admin() && $pagenow=='edit.php' && $_GET['post_type']=='catalog_product' && $_GET['s'] != '') { | |
return "DISTINCT"; | |
} | |
return $where; | |
} | |
add_filter( 'posts_distinct', 'catalog_product_search_distinct' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credits for the awnsers in http://wordpress.stackexchange.com/questions/11758/extending-the-search-context-in-the-admin-list-post-screen