Missing SQL in get_global_topic_count() function – Tags not being searched properly
-
Stumbled across a pretty nasty bug recently while setting up a BP site for a client.
Was using the bp_has_forum_topics() loop with an array of arguments in order to display a list of topics that shared a specific tag. Unfortunately, despite the fact that ‘per_page’ & ‘max’ were set to 8, and there were multiple topics that matched the criteria, only 1 topic was ever being returned.
Several hours of digging later…
It turns out that after it ran the first query properly (with type = ‘tags’ and search_terms equal to the tag in question), when BP would calculate how many more posts it needed to display (in the constructor of the BP_Forums_Template_Forum class), the code was fouling up. Eventually, after being passed around through a few functions and filters, it hits groups_total_forum_topic_count(), which does not accept parameters for $type.
This means that when the query reaches get_global_topic_count() (where the actual SQL is constructed), $search_terms is intact, but $type is not. This changes the query from searching for topics with $search_terms in the tags to one searching for topics with $search_terms in the title. This is confirmed by the fact that when the function checks for the presence of $search_terms, it adds an additional ” AND ( t.topic_title LIKE ‘%{$st}%’ )”
I was able to restore tag-searching functionality by adding an additional optional argument to the groups_total_forum_topic_count() and get_global_topic_count() functions for $type.
That, and a complicated SQL query one of my talented co-workers helped me write, set everything aright.
That section of the function went from this:
`if ( $search_terms ) {
$st = like_escape( $search_terms );
$sql .= ” AND ( t.topic_title LIKE ‘%{$st}%’ )”;
}`to this:
`if ( $search_terms ) {
$st = like_escape( $search_terms );
if($type == ‘tags’)
{
$sql .= “AND (t.topic_id
IN (
SELECT tr.object_id
FROM `wp_bb_term_relationships` tr
INNER JOIN `wp_bb_term_taxonomy` tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
INNER JOIN `wp_bb_terms` t ON t.term_id = tt.term_id
WHERE t.name LIKE ‘%{$st}%’))”;
}else
{
$sql .= ” AND ( t.topic_title LIKE ‘%{$st}%’ )”;
}
}`This is from bp-groups-classes.php (around line 640) – BuddyPress v1.5.1
Sorry if this is the wrong place for this. I’m a developer with plenty of WP experience, but I’m rather new to the BP side of things.
Anyway, this seemed like a pretty big gap in the BP core functionality – a core loop function was, eventually, losing part of its specific context and returning bad data. If this was by design, I’d be interested in knowing why… BP is a pretty huge codebase, and its hard to grasp the entirety of its scope from my limited interactions with it.
Anyway, a big thanks to the whole BP team for creating such a robust plugin. Here’s to never stopping the chase for perfection.
- The topic ‘Missing SQL in get_global_topic_count() function – Tags not being searched properly’ is closed to new replies.