Update the filter for custom fields query.
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 10 Apr 2017 20:28:37 +0000 (16:28 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 10 Apr 2017 20:28:37 +0000 (16:28 -0400)
Setting up the custom fields query. This will look for the request
variable for each custom field and put it into the query if found in the
request array.

setup/frontHooks.php

index cec957b..9f7f68e 100644 (file)
@@ -33,7 +33,7 @@ add_filter('glm-member-db-front-members-detail-sidebar', function($content, $id)
     10,
     2
 );
-    add_filter('glm-member-db-front-members-list-info', function($content, $id) {
+add_filter('glm-member-db-front-members-list-info', function($content, $id) {
         $fieldData = do_shortcode('[glm-members-fields-list member='.$id.', template="detail-sidemenu" order="title"]');
         $content .= $fieldData;
         return $content;
@@ -41,16 +41,40 @@ add_filter('glm-member-db-front-members-detail-sidebar', function($content, $id)
     10,
     2
 );
-add_filter('glm-member-db-front-search-query', function($query) {
-    // Look for the member type
+add_filter('glm-member-db-front-search-query', function($queryParts) {
+    echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+    // Get all custom fields
+    $customFields = $this->wpdb->get_results(
+        "SELECT *
+           FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX .  "custom_fields",
+        ARRAY_A
+    );
+    if ( isset( $customFields ) && count( $customFields ) > 0 ) {
+        foreach ( $customFields as $key => $field ) {
+            switch ( $field['field_type'] ) {
+            case 'text':
+            case 'textarea':
 
-    //echo '<pre>$query: ' . print_r( $query, true ) . '</pre>';
-    //$query .= " AND T.member IN (SELECT id from " . GLM_MEMBERS_PLUGIN_DB_PREFIX. "members WHERE member_type = 3 ) ";
-    //$query .= " AND T.member IN (SELECT id from " . GLM_MEMBERS_PLUGIN_DB_PREFIX. "members WHERE member_type = 2 ) ";
-    return $query;
+                break;
+            case 'checkbox':
+                // convert name to lower case and replace spaces with _
+                $field_name = str_replace( ' ', '_', strtolower( $field['field_name'] ) );
+                if ( isset( $_REQUEST[$field_name] ) && filter_var( $_REQUEST[$field_name], FILTER_VALIDATE_BOOLEAN ) ) {
+                    $queryParts[] = " T.id in (
+                        SELECT ref_dest
+                          FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data
+                         WHERE field_data = 'Yes'
+                           AND field_id = (SELECT id FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_fields WHERE field_name = '" . esc_sql( $field['field_name'] ) . "')
+                    ) ";
+                }
+                break;
+            }
+        }
+    }
+    return $queryParts;
 });
 add_filter('glm_custom_fields', function($attribute, $id) {
-    
+
     global $wpdb;
     $query = "SELECT field_data FROM " . GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX . "custom_field_data WHERE ref_dest = $id "
             . "AND field_id IN (SELECT id FROM ".GLM_MEMBERS_FIELDS_PLUGIN_DB_PREFIX."custom_fields WHERE field_name LIKE '$attribute' )";
@@ -59,4 +83,4 @@ add_filter('glm_custom_fields', function($attribute, $id) {
     },
     10,
     2
-);
\ No newline at end of file
+);