Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: table creator
- */
- /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Create Table */
- function build_table() {
- global $wpdb;
- // name the table and store in var
- $address_table = $wpdb->prefix . "address_table";
- // look for table and build if !=
- if ( $wpdb->get_var ( 'SHOW TABLES LIKE ' . $address_table ) != $address_table ) {
- $sql = 'CREATE TABLE ' . $address_table . '(
- id INTEGER(10) UNSIGNED AUTO_INCREMENT,
- hit_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- address VARCHAR(100),
- json TEXT,
- PRIMARY KEY (id) )';
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
- dbDelta($sql);
- add_option('version_num', '3.7');
- }
- }
- register_activation_hook(__FILE__, 'build_table');
- add_filter('posts_join', 'geotag_search_join' );
- add_filter('posts_where', 'geotag_search_where' );
- add_filter('posts_groupby', 'geotag_search_groupby' );
- function geotag_search_join( $join )
- {
- global $address_table, $wpdb;
- if( is_search() ) {
- $join .= " LEFT JOIN $address_table ON " .
- $wpdb->posts . ".ID = " . $address_table .
- ".id ";
- }
- return $join;
- }
- function geotag_search_where( $where )
- {
- if( is_search() ) {
- $where = preg_replace(
- "/\(\s*post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
- "(post_title LIKE $1) OR (city LIKE $1) OR (state LIKE $1) OR (zipcode LIKE $1)", $where );
- }
- return $where;
- }
- function geotag_search_groupby( $groupby )
- {
- global $wpdb;
- if( !is_search() ) {
- return $groupby;
- }
- // we need to group on post ID
- $mygroupby = "{$wpdb->posts}.ID";
- if( preg_match( "/$mygroupby/", $groupby )) {
- // grouping we need is already there
- return $groupby;
- }
- if( !strlen(trim($groupby))) {
- // groupby was empty, use ours
- return $mygroupby;
- }
- // wasn't empty, append ours
- return $groupby . ", " . $mygroupby;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement