Autocomplete relationship searching only on 1st column #1464

Closed
wreciak opened this Issue Jun 6, 2013 · 13 comments

Projects

None yet

2 participants

@wreciak
wreciak commented Jun 6, 2013

Hi
Thanks for your work.
I have relationship from advanced content type "Signatures" to advanced content type "Couples", where are data in separate columns(name, surname 1 , surname 2 etc). All of them are checked in Search Filters.
However, autocomplete is searching only in the 1st column of ""Couples" table (or in only one selected by "Display Field in Selection List"). Is there any way how to force it to search full text across all data in "Couples" table?
Thanks for any help and all your work.

@sc0ttkclark sc0ttkclark was assigned Jun 6, 2013
@sc0ttkclark
Member

I'll add a filter to let you change the lookup value, will post info on it once it's ready.

@wreciak
wreciak commented Jun 6, 2013

wau, that was fast. Thanks.

@sc0ttkclark sc0ttkclark added a commit that closed this issue Jun 7, 2013
@sc0ttkclark sc0ttkclark Fixes #1464; Adds 'pods_form_ui_field_pick_autocomplete_lookup' filte…
…r to add additional lookups
b7e15cf
@sc0ttkclark sc0ttkclark added a commit that referenced this issue Jun 7, 2013
@sc0ttkclark sc0ttkclark Update filter for #1464 2e5e68c
@sc0ttkclark
Member

Here is the implementation, you can filter the array and add additional columns, use t., d. or meta fields for pods that support it:

                        $lookup_where = array(
                            $search_data->field_index => "`t`.`{$search_data->field_index}` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'"
                        );

                        if ( $wpdb->users == $search_data->table ) {
                            $lookup_where[ 'display_name' ] = "`t`.`display_name` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                            $lookup_where[ 'user_login' ] = "`t`.`user_login` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                            $lookup_where[ 'user_email' ] = "`t`.`user_email` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                        }
                        elseif ( $wpdb->posts == $search_data->table ) {
                            $lookup_where[ 'post_title' ] = "`t`.`post_title` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                            $lookup_where[ 'post_name' ] = "`t`.`post_name` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                            $lookup_where[ 'post_content' ] = "`t`.`post_content` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                            $lookup_where[ 'post_excerpt' ] = "`t`.`post_excerpt` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                        }
                        elseif ( $wpdb->terms == $search_data->table ) {
                            $lookup_where[ 'name' ] = "`t`.`name` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                            $lookup_where[ 'slug' ] = "`t`.`slug` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                        }
                        elseif ( $wpdb->comments == $search_data->table ) {
                            $lookup_where[ 'comment_content' ] = "`t`.`comment_content` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                            $lookup_where[ 'comment_author' ] = "`t`.`comment_author` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                            $lookup_where[ 'comment_author_email' ] = "`t`.`comment_author_email` LIKE '%" . like_escape( $data_params[ 'query' ] ) . "%'";
                        }

                        $lookup_where = apply_filters( 'pods_form_ui_field_pick_autocomplete_lookup', $lookup_where, $data_params[ 'query' ], $name, $value, $options, $pod, $id, $object_params, $search_data );

                        if ( !empty( $lookup_where ) )
                            $params[ 'where' ][] = implode( ' OR ', $lookup_where );

Example of filter usage:

add_filter( 'pods_form_ui_field_pick_autocomplete_lookup', 'more_pods_autocomplete', 10, 9 );
function more_pods_autocomplete ( $lookup_where, $query, $name, $value, $options, $pod, $id, $object_params, $search_data ) {
    // Check all fields on the object
    $lookup_where[] = "t.* LIKE '%" . like_escape( $query ) . "%'";

    // If this is a WP object, table-based for it's fields, you could use this to check all of the Pods fields
    $lookup_where[] = "d.* LIKE '%" . like_escape( $query ) . "%'";

    // If this is a WP object, meta-based for it's fields, you could use this to check one of the fields
    $lookup_where[] = "my_field.meta_value LIKE '%" . like_escape( $query ) . "%'";

    return $lookup_where;
}
@wreciak
wreciak commented Jun 7, 2013

Thanks a lot.

@wreciak
wreciak commented Jul 3, 2013

Hi again
I feel really bad i cannot solve this: i would like to use this filter to change helper for autocomplete field, but i really don't know how to use a filter. I put it to my functions.php, just the line

// Check all fields on the object
    $lookup_where[] = "t.* LIKE '%" . like_escape( $query ) . "%'";

but no results were shown in the autocomplete (still searching the ID field).

I feel bad to bother you with this kind of (probably) basic things, just i really did not find the solution myself.
Thanks for any reply.
M.

@sc0ttkclark
Member

Can you confirm some info?

  • What type of Pod?
  • Table or Meta storage for the pod?
  • What types of fields are you trying to search?
@wreciak
wreciak commented Jul 3, 2013

HI

  • Advanced Content Type
  • Table storage
  • I would like to search plane text field types: code_boy, name_boy, code_girl, name_girl.
    Thanks
@sc0ttkclark
Member

What search query are you using to search?

@wreciak
wreciak commented Jul 3, 2013

I just want it to work in the default autocomplete field in form, not custom search query. Probably this is the point where i don't know what to do :/

@sc0ttkclark
Member

Can you give me a quick example of what isn't working?

When you have an item with the field value for name_girl as "Paula", does typing in "Paula" in the autocomplete not pull that item up?

@wreciak
wreciak commented Jul 3, 2013

Ok, complete scenario.
I have POD dance_couple (Advanced Content Type, Table storage) with fields: id, code_boy, name_boy, code_girl, name_girl.

I have and other POD named test (Advanced Content Type, Table storage) with field test_parejas - Relationship
› Dance couples (dance_couple) (Pod), set up as autocomplete.

When try to test > add new test and try the autocomplete field, it works with id by default, or with the field defined via magic tag in n Display Field in Selection List.
When I insert your filter (any of this three examples) in my functions.php it's searching for ever.
PODS version 2.3.8.

Thanks

@sc0ttkclark
Member

Check the AJAX request in your browser developer tools, there may be an error going on. The example code above was meant for example purposes, only one of those lines should be enabled at a time.

@wreciak
wreciak commented Jul 3, 2013

Hi
ayax is sending requests in both cases (with filter and as well without filter)
Currently i have filter like this:

add_filter( 'pods_form_ui_field_pick_autocomplete_lookup', 'more_pods_autocomplete', 10, 9 );
function more_pods_autocomplete ( $lookup_where, $query, $name, $value, $options, $pod, $id, $object_params, $search_data ) {
    // If this is a WP object, table-based for it's fields, you could use this to check all of the Pods fields
    $lookup_where[] = "d.* LIKE '%" . like_escape( $query ) . "%'";

    return $lookup_where;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment