Skip to content

Instantly share code, notes, and snippets.

@barrykooij
Last active November 6, 2017 07:54
Show Gist options
  • Save barrykooij/3e0787061a5485e86e98ac541f9eef7c to your computer and use it in GitHub Desktop.
Save barrykooij/3e0787061a5485e86e98ac541f9eef7c to your computer and use it in GitHub Desktop.
function rp4wp_exclude_category( $sql, $post_id, $post_type ) {
global $wpdb;
if ( 'post' !== $post_type ) {
return $sql;
}
// get term with slug 'past-events'
$term = get_term_by( 'slug', 'past-events', 'category' );
$term2 = get_term_by( 'slug', 'uncategoried', 'category' );
// check if we found a term with slug 'past-events'
if ( false == $term || false == $term2 ) {
return $sql;
}
$sql_replace = sprintf( "
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 NOT IN ( %s )
", " " . $term->term_id . ", " . $term2->id . " " );
return str_ireplace( 'WHERE 1=1', $sql_replace, $sql );
}
add_filter( 'rp4wp_get_related_posts_sql', 'rp4wp_exclude_category', 11, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment