Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active March 13, 2022 03:41
Show Gist options
  • Save RadGH/9ee5d3472f19644bf72ee8817c8b2516 to your computer and use it in GitHub Desktop.
Save RadGH/9ee5d3472f19644bf72ee8817c8b2516 to your computer and use it in GitHub Desktop.
WordPress get_terms replace INNER JOIN with STRAIGHT_JOIN using filters
<?php
// Step 1. Add the filters surrounding the get_terms (which should be used in your code)
add_filter( 'terms_clauses', 'rs_replace_inner_with_straight_joins', 20 );
$terms = get_terms( $args );
remove_filter( 'terms_clauses', 'rs_replace_inner_with_straight_joins', 20 );
// Step 2. Add to functions.php or similar:
function rs_replace_inner_with_straight_joins( $pieces, $taxonomies = null, $args = null ) {
global $wpdb;
$s = 'INNER JOIN ' . $wpdb->prefix;
$r = 'STRAIGHT_JOIN ' . $wpdb->prefix;
$pieces['join'] = str_replace( $s, $r, $pieces['join'] );
return $pieces;
}
@rtpHarry
Copy link

thanks for this, btw the function name doesn't match the one used in the add/remove filter

@RadGH
Copy link
Author

RadGH commented Oct 29, 2020

@rtpHarry oops! Thanks for catching that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment