Advertisement
demetriusPop

join table

Dec 12th, 2013
1,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. Plugin Name: table creator
  6.  
  7. */
  8.  
  9.  
  10. /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Create Table */
  11. function build_table() {
  12.  
  13. global $wpdb;
  14.  
  15. // name the table and store in var
  16. $address_table = $wpdb->prefix . "address_table";
  17.  
  18. // look for table and build if !=
  19. if ( $wpdb->get_var ( 'SHOW TABLES LIKE ' . $address_table ) != $address_table ) {
  20.  
  21. $sql = 'CREATE TABLE ' . $address_table . '(
  22. id INTEGER(10) UNSIGNED AUTO_INCREMENT,
  23. hit_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  24. address VARCHAR(100),
  25. json TEXT,
  26. PRIMARY KEY (id) )';
  27.  
  28. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  29.  
  30. dbDelta($sql);
  31.  
  32. add_option('version_num', '3.7');
  33.  
  34. }
  35.  
  36. }
  37. register_activation_hook(__FILE__, 'build_table');
  38.  
  39.  
  40.  
  41. add_filter('posts_join', 'geotag_search_join' );
  42. add_filter('posts_where', 'geotag_search_where' );
  43. add_filter('posts_groupby', 'geotag_search_groupby' );
  44.  
  45. function geotag_search_join( $join )
  46. {
  47. global $address_table, $wpdb;
  48.  
  49. if( is_search() ) {
  50. $join .= " LEFT JOIN $address_table ON " .
  51. $wpdb->posts . ".ID = " . $address_table .
  52. ".id ";
  53. }
  54.  
  55. return $join;
  56. }
  57.  
  58. function geotag_search_where( $where )
  59. {
  60. if( is_search() ) {
  61. $where = preg_replace(
  62. "/\(\s*post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
  63. "(post_title LIKE $1) OR (city LIKE $1) OR (state LIKE $1) OR (zipcode LIKE $1)", $where );
  64. }
  65.  
  66. return $where;
  67. }
  68.  
  69. function geotag_search_groupby( $groupby )
  70. {
  71. global $wpdb;
  72.  
  73. if( !is_search() ) {
  74. return $groupby;
  75. }
  76.  
  77. // we need to group on post ID
  78.  
  79. $mygroupby = "{$wpdb->posts}.ID";
  80.  
  81. if( preg_match( "/$mygroupby/", $groupby )) {
  82. // grouping we need is already there
  83. return $groupby;
  84. }
  85.  
  86. if( !strlen(trim($groupby))) {
  87. // groupby was empty, use ours
  88. return $mygroupby;
  89. }
  90.  
  91. // wasn't empty, append ours
  92. return $groupby . ", " . $mygroupby;
  93. }
  94.  
  95. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement