Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active November 13, 2015 14:59
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 tommcfarlin/926f78b195f94c93fdc4 to your computer and use it in GitHub Desktop.
Save tommcfarlin/926f78b195f94c93fdc4 to your computer and use it in GitHub Desktop.
[WordPress] An example for how to JOIN two tables using the WordPress API.
<?php
add_filter( 'posts_join', 'acme_search_submission_join' );
/**
* Performs a JOIN on the post and the post meta tables so that we can retrieve results
* that also include data stored in the post meta table.
*
* @param string $join The initial JOIN clause.
* @return string $join The clause for joining the post and the post meta tables, if on the search template.
*/
function acme_search_submission_join( $join ) {
if ( is_admin() || ! is_search() ) {
return $join;
}
global $wpdb;
$join .= "JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
return $join;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment