Group Mods

  • Profile picture of @mercime
  • Profile picture of r-a-y
  • Profile picture of Hugo

Missing SQL in get_global_topic_count() function – Tags not being searched properly (5 posts)

Started 5 months, 2 weeks ago by: info_sponge

  • Profile picture of info_sponge info_sponge said 5 months, 2 weeks ago:

    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['where'] .= " AND (  t.topic_title LIKE '%{$st}%' )";
    }

    to this:

    if ( $search_terms ) {
    	$st = like_escape( $search_terms );
    	if($type == 'tags')
    	{
    		$sql['where'] .= "AND (t.topic_id
    			IN (
    			SELECT tr.object_id
    			FROM <code>wp_bb_term_relationships</code> tr
    			INNER JOIN <code>wp_bb_term_taxonomy</code> tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
    			INNER JOIN <code>wp_bb_terms</code> t ON t.term_id = tt.term_id
    			WHERE t.name LIKE '%{$st}%'))";
    	}else
    	{
    		$sql['where'] .= " 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.

  • Profile picture of info_sponge info_sponge said 5 months, 2 weeks ago:

    Woops, the backticks in my SQL query got converted into and blocks inside the actual code block…

  • Profile picture of Paul Gibbs Paul Gibbs said 5 months, 2 weeks ago:

    I haven’t looked at this, but it sounds as if you have. Can you post a bug report on http:/BuddyPress.trac.WordPress.org ? Thanks

  • Profile picture of info_sponge info_sponge said 5 months, 2 weeks ago:

    Sure thing! Like I said, new to BuddyPress community – not sure of what the best protocol was.

    Ticket submitted

  • Profile picture of Paul Gibbs Paul Gibbs said 5 months, 2 weeks ago:

    Cheers :)