Can you please add additional query_add_select/join/where filters?

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Can you please add additional query_add_select/join/where filters?

This topic contains 4 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 1 year, 5 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #35111
    nickchomey18
    nickchomey18
    Participant

    I notice that you offer filters for query_add_select/join/where in the CPT, Index Table, Terms and Users search helpers, allowing us to add additional statements to the queries. This is extremely useful. Its a shame it took me so long to find it in the code – perhaps it would be worth adding to the documentation/knowledge base?

    Anyway, I am hoping that you can please add the same select/join/where filters to the BuddyPress search helper (or, better yet, all of the helpers) so that I can easily make some modifications to the searches. This would be very helpful because I would like to add code that respects various privacy settings in BuddyBoss, such as whether

    • someone is your friend
    • you are a member of a private or hidden group
    • the specific post is set to be viewable by all visitors, logged-in users only, friends only, or just yourself
    • someone has been suspended
    • etc…

    Additionally, BuddyBoss uses some extra tables for document, image, and video uploads, which creates tables like bp_activity_meta, bp_document, bp_document_meta, etc…

    I don’t expect you to support any of this, but these extra filters would allow me to cleanly add the required code from external snippets. I imagine that this would not be much work – just a few extra lines in each search helper – and it shouldn’t affect anyone who chooses not to use them.

    Thanks!

    p.s. I should also mention that I think that MANY BuddyPress and, especially, BuddyBoss users would buy ASP if you added more comprehensive support for these features, privacy, etc… because the built-in BB forum search functionality is awful, there’s no file content support, can’t create your own search forms with filters etc…

    #35149
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Finally I had the time to inspect the source code, as I recalled I had some sort of a solution for this already, I just was not sure.

    So initially I have added the query_add_select/join/etc.. hooks, but later on, when the plugin was extended, we figured it might be a bit too much of a code overhead, so we wanted to merge all of these into a single hook. The current solution is, that extra query arguments can be added via the asp_query_args filter – and the good news is, that it is also available for BuddyPress. Unfortunately I completely forgot to document this (and forgot about it as well), and the older version of the filters was not documented on purpose (so we can switch to this new solution, and people won’t use the old ones).

    So, all you need is to use the asp_query_args hook. And then, there are these array keys for the $args variable (taken from the source):

    // ----------------------------------------------------------------
    // 10. QUERY FIELDS
    // ----------------------------------------------------------------
    'cpt_query' => array(
    	'fields' => '',
    	'join' => '',
    	'where' => '',
    	'orderby' => '',
    	'groupby' => ''
    ),
    'term_query' => array(
    	'fields' => '',
    	'join' => '',
    	'where' => '',
    	'orderby' => '',
    	'groupby' => ''
    ),
    'user_query' => array(
    	'fields' => '',
    	'join' => '',
    	'where' => '',
    	'orderby' => '',
    	'groupby' => ''
    ),
    'attachment_query' => array(
    	'fields' => '',
    	'join' => '',
    	'where' => '',
    	'orderby' => '',
    	'groupby' => ''
    ),
    'buddypress_groups_query' => array(
    	'fields' => '',
    	'join' => '',
    	'where' => '',
    	'orderby' => '',
    	'groupby' => ''
    ),
    'buddypress_activities_query' => array(
    	'fields' => '',
    	'join' => '',
    	'where' => '',
    	'orderby' => '',
    	'groupby' => ''
    ),

    From here, this gets very simple. In a hook on asp_query_args, you can basically do anything:

    global $wpdb;
    $args['buddypress_activities_query']['fields'] = 'my_custom_row as row,';
    $args['buddypress_activities_query']['join'] = "LEFT JOIN my_table on $wpdb->prefix" . ".bp_activity.user_id";
    $args['buddypress_activities_query']['where'] = "AND (my_field.xyz = $wpdb->prefix" . ".bp_activity.user_id)";

    ..and so on, I’m sure you get the point.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #35154
    nickchomey18
    nickchomey18
    Participant

    Very slick! That’s much easier than using numerous hooks, functions etc… I’m sure that I can figure out what I need from this. Thanks very much!

    Having said that, I might not even use this method anymore. Given that ASP only uses the Index Table for posts in wp_posts, ASP’s BuddyPress search is effectively the same as BuddyBoss’: wpdb->get_results. They have a tremendous amount of redundancy in their code (Query Monitor tells me that they have something like 100 duplicate queries…), but it is all set up to do the filtering for friend, group and post privacy; media such as documents, photos and videos; and also has the results formatted for their HTML generation.

    So, at least for now, I’ll probably just use ASP for Index Table post types, pass the results to BB through creating a global variable with the ‘asp_results’ filter, format those results as needed for BB, and then perform the other searches with BB’s search. I’ll have to do some modifications for bbpress Topic and Reply results, which are in the Index Table, but it will be far less than re-creating all of the other stuff.

    But perhaps later (both out of curiosity and to streamline everything) I will use the asp_query_args that you’ve provided to move everything over to ASP

    Thanks again for your help. Hopefully you’ll soon be able to document these args for the sake of others.

    P.s. Again, if you’re curious about integrating more of the BuddyBoss stuff (respect privacy, documents etc…) into ASP, the code is all open-source here: https://github.com/buddyboss/buddyboss-platform/tree/release/src/bp-search.

    And, for example, here is the BuddyPress activities code, and if you look at lines 69-118, 139-142, you’ll see the code for filtering for group, friend, and post privacy. On closer inspection, it is not particularly complicated. It could (and should) be made into a function in the main class so that it isn’t called so many times. https://github.com/buddyboss/buddyboss-platform/blob/release/src/bp-search/classes/class-bp-search-activities.php

    Anyway, I am quite confident that many BuddyBoss users would buy ASP if this was integrated, to say nothing of BuddyPress users who don’t have any search functionality. Because, as it stands, the ASP BuddyPress search is not particularly useful without being able to adequately respect privacy.

    #35191
    nickchomey18
    nickchomey18
    Participant

    I have found some bugs in this mechanism.

    1. The attachment_query arguments don’t work with an index table search. Line 37 of ajax-search-pro\includes\classes\search\class-asp-search-attachments.php replaces the rest of the function with class-asp-search-indextable.php, which only checks for $args[‘cpt_query’]

    I did the following, but perhaps you will have a better solution for the next update.

    
    if ($words == "attachment"){
        if ( isset($args['attachment_query']) && is_array($args['attachment_query']) ) {
        $this->query = str_replace(
            array('{args_fields}', '{args_join}', '{args_where}', '{args_orderby}'),
            array($args['attachment_query']['fields'], $args['attachment_query']['join'], $args['attachment_query']['where'], $args['attachment_query']['orderby']),
            $this->query
        );
        } else {
            $this->query = str_replace(
                array('{args_fields}', '{args_join}', '{args_where}', '{args_orderby}'),
                '',
                $this->query
            );
        }
        if ( isset($args['attachment_query'], $args['attachment_query']['groupby']) && $args['attachment_query']['groupby'] != '' ) {
            $this->query = str_replace('{args_groupby}', $args['attachment_query']['groupby'], $this->query);
        } else {
            $this->query = str_replace('{args_groupby}', "$wpdb->posts.ID", $this->query);
        }
    }	
    else {
        if ( isset($args['cpt_query']) && is_array($args['cpt_query']) ) {
            $_mod_q = $args['cpt_query'];
            foreach ($_mod_q as $qk => $qv) {
                $_mod_q[$qk] = str_replace($wpdb->posts . '.ID', 'asp_index.doc', $_mod_q[$qk]);
            }
            $this->query = str_replace(
                array('{args_fields}', '{args_join}', '{args_where}', '{args_orderby}'),
                array($_mod_q['fields'], $_mod_q['join'], $_mod_q['where'], $_mod_q['orderby']),
                $this->query
            );						
        } 
        else {
            $this->query = str_replace(
                array('{args_fields}', '{args_join}', '{args_where}', '{args_orderby}'),
                '',
                $this->query
            );
        }					
    }

    2. I get the following error (excerpted) :

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘DISTINCT asp_index.doc as id, asp_index.blogid as blogid, ‘pagepost’ a’ at line 4]
    SELECT pm.meta_value as activityid, DISTINCT asp_index.doc as id, asp_index.blogid as blogid, ‘pagepost’ as

    The problem being with the Improved title query str_replace – it adds DISTINCT to the query, but the {args_fields} and $add_select hooks are inserted before that.

    I fixed it by moving these to the bottom of the SELECT section, before FROM, but I don’t know if this is the appropriate place to do it. I had to move the comma in the snippet you provided above to be at the front rather than trailing.

    #35225
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Thank you! I will look into these.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.