Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin

tommcfarlin/00-join-two-tables.php Secret

Last active Nov 13, 2015
Embed
What would you like to do?
[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
You can’t perform that action at this time.