Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. add_filter( 'posts_join', 'property_search_join' );
  2. function property_search_join ( $join ) {
  3. global $pagenow, $wpdb;
  4.  
  5. // I want the filter only when performing a search on edit page of Custom Post Type named "property".
  6. if ( is_admin() && 'edit.php' === $pagenow && 'property' === $_GET['post_type'] && ! empty( $_GET['s'] ) ) {
  7. $join .= 'LEFT JOIN ' . $wpdb->postmeta . ' ON ' . $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
  8. }
  9. return $join;
  10. }
  11.  
  12. add_filter( 'posts_where', 'property_search_where' );
  13. function property_search_where( $where ) {
  14. global $pagenow, $wpdb;
  15.  
  16. // I want the filter only when performing a search on edit page of Custom Post Type named "property".
  17. if ( is_admin() && 'edit.php' === $pagenow && 'property' === $_GET['post_type'] && ! empty( $_GET['s'] ) ) {
  18. $where = preg_replace(
  19. "/\(\s*" . $wpdb->posts . ".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
  20. "(" . $wpdb->posts . ".post_title LIKE $1) OR (" . $wpdb->postmeta . ".meta_value LIKE $1)", $where );
  21. }
  22. return $where;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement