Skip to content

Instantly share code, notes, and snippets.

@wvega
Created March 17, 2016 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wvega/451f524f598d344c8d15 to your computer and use it in GitHub Desktop.
Save wvega/451f524f598d344c8d15 to your computer and use it in GitHub Desktop.
Code used to create SQL clauses for meta queries using custom tables (Draft)
<?php
$regions_query = new WP_Meta_Query( $query_object->query['_regions_query'] );
$regions_clauses = $regions_query->get_sql( 'post', $this->db->posts, 'ID', $query_object );
$regions_clauses['join'] = str_replace( $this->db->postmeta, AWPCP_TABLE_AD_REGIONS, $regions_clauses['join'] );
$regions_clauses['join'] = preg_replace( "/mt(\d+)/", 'listing_regions\1', $regions_clauses['join'] );
$regions_clauses['join'] = str_replace( 'post_id', 'ad_id', $regions_clauses['join'] );
$regions_clauses['where'] = str_replace( $this->db->postmeta, AWPCP_TABLE_AD_REGIONS, $regions_clauses['where'] );
$regions_clauses['where'] = preg_replace( "/mt(\d+)/", 'listing_regions\1', $regions_clauses['where'] );
$regions_clauses['where'] = preg_replace(
"/(\w+).meta_key = '(\w+)' AND CAST\([^)]+\) (LIKE '(?:\\\'|[^'])+')/",
'$1.$2 $3',
$regions_clauses['where'],
-1,
$count
);
$clauses['join'] = $clauses['join'] . $regions_clauses['join'];
$clauses['where'] = $clauses['where'] . $regions_clauses['where'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment