Skip to content

Instantly share code, notes, and snippets.

@barrykooij
Last active January 22, 2016 15:33
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 barrykooij/ae50b7b0a65aa4d0365a to your computer and use it in GitHub Desktop.
Save barrykooij/ae50b7b0a65aa4d0365a to your computer and use it in GitHub Desktop.
Related Posts for WordPress force same category
function rp4wp_force_same_category( $sql, $post_id, $post_type ) {
global $wpdb;
if ( 'post' !== $post_type ) {
return $sql;
}
$sql_replace = "
INNER JOIN " . $wpdb->term_relationships . " ON (R.`post_id` = " . $wpdb->term_relationships . ".object_id)
INNER JOIN " . $wpdb->term_taxonomy . " ON (" . $wpdb->term_relationships . ".term_taxonomy_id = " . $wpdb->term_taxonomy . ".term_taxonomy_id)
WHERE 1=1
AND " . $wpdb->term_taxonomy . ".taxonomy = 'category'
AND " . $wpdb->term_taxonomy . ".term_id IN ( SELECT TT.term_id FROM " . $wpdb->term_taxonomy . " TT INNER JOIN " . $wpdb->term_relationships . " TR ON TR.term_taxonomy_id = TT.term_taxonomy_id WHERE TR.object_id = " . $post_id . " )
";
return str_ireplace( 'WHERE 1=1', $sql_replace, $sql );
}
add_filter( 'rp4wp_get_related_posts_sql', 'rp4wp_force_same_category', 11, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment