a simple and Not perfect solution which will do most of the job for you is to overwrite the table names like this:
function custom_prefix_change($sql) {
global $wpdb,$wp;
$prefix = $wpdb->prefix;
if(is_single()){
//check if the post exists in the genuine posts table
$results = $wpdb->get_results($sql,"ARRAY_A");
if($results[0]['ID']){
//if exists then return
return $sql;
}else{
//else change the query
$sql = str_replace("$wpdb->posts","{$wpdb->prefix}custom_posts",$sql);
$wpdb->term_relationships = $wpdb->prefix . 'custom_term_relationships';
$wpdb->postmeta = $wpdb->prefix . 'custom_postmeta';
}
}
return $sql;
}
Now for a complete solution you will need to replace the tables in the terms_clauses
filter hook for get_terms
and get the meta data from the custom table yourself using the get_post_metadata
for get_post_meta
.