A hook each for both JOIN and WHERE SQL would be great.
-
I’m using this plugin in a unique situation, however I think others may see the benefit in this ability.
Basically there is no way to add a custom JOIN and WHERE to the SQL query.
In my case I use a custom taxonomy to show specific content on a subdomain, so I only want related results for that subdomain. You might have a custom taxonomy, and only want to see related posts for the taxonomy terms set for that post. Could even be used with the built in ones, to only show posts with the same categories or tags, for example.
I haven’t made the hook, just add a function I’m using in my functions.php directly, but it means the plugin can’t be updated. Yoast WordPress SEO allows a hook for the JOIN and WHERE SQL on his sitemaps – I use the same functions here.
// Line 244 of Contextual Related Posts (after the FROM ".$wpdb->posts.") " . tsg_join('', 'post') . " // Line 250 of Contextual Related Posts (after AND ID != %d) " . tsg_where('', 'post') . " // functions.php -- I haven't tested this, I've changed this from // what I do, to probably limit the related post results to only // within the taxonomy terms assigned to the post you're viewing. function tsg_join( $join_filter, $post_type ) { global $wpdb; if ( $join_filter === false ) $join_filter = ''; return $join_filter . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) "; } function tsg_where( $where_filter, $post_type ) { global $wpdb; global $post; if ( $where_filter === false ) $where_filter = ''; $terms = wp_get_post_terms( $post->ID, 'custom-taxonomy' ); $term_ids = array(); foreach( $terms as $term ) $term_ids[] = $term->term_id; return $where_filter . " AND $wpdb->term_taxonomy.taxonomy = 'custom-taxonomy' AND $wpdb->term_taxonomy.term_id IN (" . implode(',', array_unique($term_ids) ) . ") "; }
This code was based on 1.9.1 of Contextual Related Posts.
- The topic ‘A hook each for both JOIN and WHERE SQL would be great.’ is closed to new replies.