Title: Feature Request &#8211; Make Custom Fields Sortable
Last modified: September 5, 2018

---

# Feature Request – Make Custom Fields Sortable

 *  ResolvedPlugin Contributor [sburdett](https://wordpress.org/support/users/sburdett/)
 * (@sburdett)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/feature-request-make-custom-fields-sortable/)
 * Not Sure if there is a better place for me to post this stuff (Github?) but here
   is another feature recommendation for when you have time. It would be nice to
   have the ability to sort by custom fields. I went ahead and wrote up the code
   for this ( I hate when people just ask for things but are not willing to do the
   work) so here it is in case you decide to implement it:
 * Code for the “Other Settings” Section of the Settings Page:
    **cloud-search/admin/
   cloud-search-admin-settings.php Line 550**
 *     ```
       <tr valign="top">
              <th scope="row"><label for="acs_allow_sortable_fields"><?php _e( 'Allow Sort By custom fields', ACS::PREFIX ) ?></label></th>
              <td>
                    <input type="checkbox" id="acs_allow_sortable_fields" name="acs_allow_sortable_fields" value="1" <?php echo ( $settings->acs_allow_sortable_fields == 1 ) ? 'checked="checked"' : '' ?> />&nbsp;
                           <span class="acs_inline_tips"><?php _e( 'Mark Custom Fields as sortable in CloudSearch', ACS::PREFIX ) ?></span>
              </td>
       </tr>
       ```
   
 * **cloud-search/admin/cloud-search-admin-settings.php Line 664**
 *     ```
       $settings->acs_allow_sortable_fields =  ( !empty( $_POST[ 'acs_allow_sortable_fields' ] ) ) ? 1 : 0;
       ```
   
 * Code for the Search Schema:
    **cloud-search/cloud-search-schema.php Line 270**
 *     ```
       // Check if custom fields are sortable
       $acs_sortable_fields =  ( !empty($settings->acs_allow_sortable_fields) && $settings->acs_allow_sortable_fields == 1 ) ? true : false;
       ```
   
 * **cloud-search/cloud-search-schema.php Line 290**
 *     ```
       'SortEnabled' => $acs_sortable_fields,
       ```
   
 * This will only work if the fields have not been created yet, I guess you could
   add a button to the manage page to update field options or something. This may
   also be good once you can select the Analysis Scheme Language. If you want me
   to code it to save you some time let me know. I really appreciate the plugin 
   and will help out if I can.
    -  This topic was modified 7 years, 8 months ago by [sburdett](https://wordpress.org/support/users/sburdett/).
    -  This topic was modified 7 years, 8 months ago by [sburdett](https://wordpress.org/support/users/sburdett/).

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Contributor [sburdett](https://wordpress.org/support/users/sburdett/)
 * (@sburdett)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/feature-request-make-custom-fields-sortable/#post-10664798)
 * So I realized that text fields don’t always sort so well (Especially if the original
   values are integers or double) So this may be a better option. I added three 
   fields on the settings page that take a csv list of fields. One is for int field,
   one for double and one for sortable fields. The Code changes for this are:
 * Code for the “Other Settings” Section of the Settings Page:
    **cloud-search/admin/
   cloud-search-admin-settings.php Line 514 add**
 *     ```
       <tr valign="top">
       	<th scope="row"><label for="acs_schema_fields_int"><?php _e( 'Custom field types INT', ACS::PREFIX ) ?></label></th>
       	<td>
               <input type="text" id="acs_schema_fields_int" name="acs_schema_fields_int" value="<?php echo $settings->acs_schema_fields_int ?>" class="big" />
               <div class="acs_row_tips">
                   <span><?php _e( 'Enter Comma Separated List of fields to be converted to INT in CloudSearch.', ACS::PREFIX ) ?></span>
               </div>
           </td>
       </tr>
       <tr valign="top">
       	<th scope="row"><label for="acs_schema_fields_double"><?php _e( 'Custom field types Double', ACS::PREFIX ) ?></label></th>
       	<td>
               <input type="text" id="acs_schema_fields_double" name="acs_schema_fields_double" value="<?php echo $settings->acs_schema_fields_double ?>" class="big" />
               <div class="acs_row_tips">
                   <span><?php _e( 'Enter Comma Separated List of fields to be converted to Double in CloudSearch.', ACS::PREFIX ) ?></span>
               </div>
           </td>
       </tr>
       <tr valign="top">
       	<th scope="row"><label for="acs_schema_fields_sortable"><?php _e( 'Custom field types Sortable', ACS::PREFIX ) ?></label></th>
       	<td>
               <input type="text" id="acs_schema_fields_sortable" name="acs_schema_fields_sortable" value="<?php echo $settings->acs_schema_fields_sortable ?>" class="big" />
               <div class="acs_row_tips">
                   <span><?php _e( 'Enter Comma Separated List of fields to be Sortable in CloudSearch.', ACS::PREFIX ) ?></span>
               </div>
           </td>
       </tr>
       ```
   
 * **cloud-search/admin/cloud-search-admin-settings.php Line 665 add**
 *     ```
       $settings->acs_schema_fields_int = ( !empty( $_POST[ 'acs_schema_fields_int' ] ) ) ? wp_kses_post( $_POST[ 'acs_schema_fields_int' ] ) : '';
       $settings->acs_schema_fields_double = ( !empty( $_POST[ 'acs_schema_fields_double' ] ) ) ? wp_kses_post( $_POST[ 'acs_schema_fields_double' ] ) : '';
       $settings->acs_schema_fields_sortable = ( !empty( $_POST[ 'acs_schema_fields_sortable' ] ) ) ? wp_kses_post( $_POST[ 'acs_schema_fields_sortable' ] ) : '';
       ```
   
 * Code for the Search Schema:
    **cloud-search/cloud-search-schema.php Line 261 
   change function acs_get_custom_index_fields() to:**
 *     ```
       function acs_get_custom_index_fields() {
       	$fields = array();
   
       	// Get settings option
       	$settings = ACS::get_instance()->get_settings();
   
       	// Define custom index fields (adding custom fields)
       	$acs_schema_fields = $settings->acs_schema_fields;
   
       	//Get Custom Field Parameters
               $acs_int_fields = array_map('trim',str_getcsv($settings->acs_schema_fields_int));
               $acs_double_fields = array_map('trim',str_getcsv($settings->acs_schema_fields_double));
               $acs_sortable_fields = array_map('trim',str_getcsv($settings->acs_schema_fields_sortable));
   
       	if ( ! empty ( $acs_schema_fields ) ) {
       		// If there are some custom fields
       		$acs_schema_fields = explode( ACS::SEPARATOR, $acs_schema_fields );
   
       		// Loop custom fields
       		foreach ( $acs_schema_fields as $acs_schema_field ) {
       			// Replace field slug "-" with "_" due to Amazon CloudSearch valid pattern rule
       			$acs_schema_field_clean = str_replace( '-', '_', $acs_schema_field );
   
                   // Check if Field is INT or Double
                   if (isset($acs_int_fields) && in_array($acs_schema_field,$acs_int_fields)) {
                       $acs_option_key_field = 'IntOptions';
                   } else if (isset($acs_double_fields) && in_array($acs_schema_field,$acs_double_fields)) {
                       $acs_option_key_field = 'DoubleOptions';
                   } else {
                       $acs_option_key_field = 'TextOptions';
                   }
   
                   // Check if the field is sortable
   
                   $acs_sort_enabled_field = (isset($acs_sortable_fields) && in_array($acs_schema_field,$acs_sortable_fields)) ? true : false;
   
       			// By default all schema fields are indexed as a text string and they are not enabled for facet, highlight or sort
       			$fields[ ACS::CUSTOM_FIELD_PREFIX . $acs_schema_field_clean ] = array(
       				'type' => 'text',
       				'option_key' => $acs_option_key_field,
       				'option_value' => array(
       					'FacetEnabled' => false,
       					'SearchEnabled' => true,
       					'ReturnEnabled' => true,
       					'SortEnabled' => $acs_sort_enabled_field,
       					'HighlightEnabled' => false,
       					'AnalysisScheme' => ACS::ANALYSIS_SCHEMA
       				)
       			);
       		}
       	}
       ```
   
 * Code for the Indexer (To Ensure Data Is Valid):
    **cloud-search/cloud-search-
   indexer.php Line 36 change function acs_prepare_document() to:**
 *     ```
       function acs_prepare_document( $post, $from_save_transaction = false ) {
           try {
               // Get settings option
               $settings = ACS::get_instance()->get_settings();
   
               // Get author
               $post_author = $post->post_author;
               $post_author_info = get_userdata($post_author);
               $post_author_name = $post_author_info->display_name;
               if ( empty( $post_author_name ) ) $post_author_name = '-';
   
               // Get default taxonomies
               $taxonomy_category = acs_get_term_list( $post->ID, 'category' );
               $taxonomy_tag = acs_get_term_list( $post->ID, 'post_tag' );
   
               // Get custom taxonomies
               $taxonomies_custom = array();
               $acs_schema_taxonomies = $settings->acs_schema_taxonomies;
   
               if ( ! empty ( $acs_schema_taxonomies ) ) {
                   // If there are some custom taxonomies
                   $acs_schema_taxonomies = explode( ACS::SEPARATOR, $acs_schema_taxonomies );
   
                   // Loop custom taxonomies
                   foreach ( $acs_schema_taxonomies as $acs_schema_taxonomy ) {
                       // Get current post custom taxonomy values
                       $taxonomy_custom = acs_get_term_list( $post->ID, $acs_schema_taxonomy );
   
                       // Replace taxonomy slug "-" with "_" due to Amazon CloudSearch valid pattern rule
                       $acs_schema_taxonomy_clean = str_replace( '-', '_', $acs_schema_taxonomy );
   
                       $taxonomies_custom[ ACS::CUSTOM_TAXONOMY_PREFIX . $acs_schema_taxonomy_clean ] = $taxonomy_custom;
                   }
               }
   
               // Get custom fields
               $fields_custom = array();
               $acs_schema_fields = $settings->acs_schema_fields;
   
               //Get Custom Field Parameters
               $acs_int_fields = array_map('trim',str_getcsv($settings->acs_schema_fields_int));
               $acs_double_fields = array_map('trim',str_getcsv($settings->acs_schema_fields_double));
   
   
               if ( ! empty ( $acs_schema_fields ) ) {
                   // If there are some custom fields
                   $acs_schema_fields = explode( ACS::SEPARATOR, $acs_schema_fields );
   
                   // Loop custom fields
                   foreach ( $acs_schema_fields as $acs_schema_field ) {
                       // Get current post custom field values
                       if ( $from_save_transaction && isset( $_POST[ $acs_schema_field ] ) ) {
                           // Read from request POST
                           $field_custom = $_POST[ $acs_schema_field ];
                       }
                       else {
                           // Read from post meta
                           $field_custom = get_post_meta( $post->ID, $acs_schema_field, true );
                       }
                       // Verfy & Convert INT and Double Fields
                       if (isset($acs_int_fields) && in_array($acs_schema_field,$acs_int_fields)) {
                           $field_custom = intval($field_custom);
                       } else if (isset($acs_double_fields) && in_array($acs_schema_field,$acs_double_fields)) {
                           $field_custom = doubleval($field_custom);
                       }
   
                       // Replace field slug "-" with "_" due to Amazon CloudSearch valid pattern rule
                       $acs_schema_field_clean = str_replace( '-', '_', $acs_schema_field );
   
       	            if ( ! empty( $field_custom ) ) $fields_custom[ ACS::CUSTOM_FIELD_PREFIX . $acs_schema_field_clean ] = $field_custom;
                   }
               }
   
       	    $post_image = '';
       	    if ( ! empty( $settings->acs_schema_fields_custom_image_id ) && is_plugin_active( 'multiple-post-thumbnails/multi-post-thumbnails.php' ) ) {
       		    // Retrieve post image from "Multiple Post Thumbnails" plugin if a image id is provided and plugin is active
       		    $post_image = MultiPostThumbnails::get_post_thumbnail_url( $post->post_type, $settings->acs_schema_fields_custom_image_id, $post->ID );
       	    }
       	    else if ( ! empty( $settings->acs_schema_fields_image_size ) ) {
       		    // Retrieve post image if a image size name is provided
       		    $image_object = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), $settings->acs_schema_fields_image_size );
   
       		    if ( ! empty( $image_object ) && ! empty( $image_object[0] ) && $image_object[0] != '' ) {
       			    // Found image, use it
       			    $post_image = $image_object[0];
       		    }
       	    }
   
       	    // Prepare fields array (merging default fields and custom fields/taxonomies
               $fields = array(
                   'site_id' => acs_get_site_id(),
                   'blog_id'=> acs_get_blog_id(),
                   'id' => $post->ID,
                   'post_type' => $post->post_type,
                   'post_status' => $post->post_status,
                   'post_format' => get_post_format( $post->ID ),
                   'post_title' => $post->post_title,
                   'post_content' => $post->post_content,
                   'post_excerpt' => $post->post_excerpt,
                   'post_url' => get_permalink($post->ID),
                   'post_image' => $post_image,
                   'post_date' => strtotime( $post->post_date ),
                   'post_date_gmt' => strtotime( $post->post_date_gmt ),
                   'post_modified' => strtotime( $post->post_modified ),
                   'post_modified_gmt' => strtotime( $post->post_modified_gmt ),
                   'post_author' => $post_author,
                   'post_author_name' => $post_author_name,
                   'category' => $taxonomy_category,
                   'tag' => $taxonomy_tag
               );
               if ( ! empty( $taxonomies_custom ) ) $fields = array_merge( $fields, $taxonomies_custom );
               if ( ! empty( $fields_custom ) ) $fields = array_merge( $fields, $fields_custom );
   
               // Manipulate standard fields (in your sub-theme add a filter "cloud_search_<POST_TYPE>_fields" that adds all necessary fields of your theme)
               $fields = apply_filters( "cloud_search_{$post->post_type}_fields", $fields, $post, $from_save_transaction );
   
               // Prepare doc object
               $doc = array(
                   'type' => 'add',
                   'id' => acs_get_document_key( $post->ID ),
                   'fields' => $fields
               );
   
               return $doc;
           }
           catch ( \Exception $e ) {
               return null;
           }
       }
       ```
   
 * I could add the other field types (Double Array, Int Array, Date, ect.) if you
   want but This is all I needed so Unless you wanted me to do it I did not see 
   the point.
    -  This reply was modified 7 years, 8 months ago by [sburdett](https://wordpress.org/support/users/sburdett/).
 *  Plugin Contributor [sburdett](https://wordpress.org/support/users/sburdett/)
 * (@sburdett)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/feature-request-make-custom-fields-sortable/#post-10665287)
 * The above Code has some bugs in it. Rather than continuously repairing it here
   I created a GitHub repo and just put the modified files in there.
 * [https://github.com/shaneburdett/cloudsearch_changes](https://github.com/shaneburdett/cloudsearch_changes)
 *  Plugin Author [Andrea Landonio](https://wordpress.org/support/users/lando1982/)
 * (@lando1982)
 * [7 years, 8 months ago](https://wordpress.org/support/topic/feature-request-make-custom-fields-sortable/#post-10688276)
 * Hi Shane! You’re fantastic!! Many thanks for all your code! Very appreciated!
   
   Unfortunately WordPress use SVN for his repository, not GIT.. the sources are
   under SVN and (in this case) I use only SVN for code versioning… though I like
   GIT.. 😀
 * I think that on tuesday 18 I’ll release a new plugin version (with terms support).
   After that I’ll work on many little issues that users reported to me and I’m 
   also eager to integrate all your suggestions..
 * Keep in touch and thanks again!!!
 *  Plugin Author [Andrea Landonio](https://wordpress.org/support/users/lando1982/)
 * (@lando1982)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/feature-request-make-custom-fields-sortable/#post-10781590)
 * Issue online 😉
 *  Plugin Contributor [sburdett](https://wordpress.org/support/users/sburdett/)
 * (@sburdett)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/feature-request-make-custom-fields-sortable/#post-10782978)
 * Awsome! Loving the plugin.
    -  This reply was modified 7 years, 7 months ago by [sburdett](https://wordpress.org/support/users/sburdett/).
 *  Plugin Contributor [sburdett](https://wordpress.org/support/users/sburdett/)
 * (@sburdett)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/feature-request-make-custom-fields-sortable/#post-10857220)
 * Should I go ahead and add other field types (date, date-array, double-array, 
   int-array, latlon, literal, text-array)? I did not need them for my use case 
   but now that you incorporated this feature it may be nice to have for other use
   cases. Would you like me to code it?
 *  Plugin Author [Andrea Landonio](https://wordpress.org/support/users/lando1982/)
 * (@lando1982)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/feature-request-make-custom-fields-sortable/#post-10864096)
 * Hi Shane, I’ve taken a note about it but if you want to speed up the release 
   feel free to help me writing some code..
    Always welcome 😉 Bye

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Feature Request – Make Custom Fields Sortable’ is closed to new replies.

 * ![](https://ps.w.org/cloud-search/assets/icon-256x256.png?rev=1350439)
 * [CloudSearch](https://wordpress.org/plugins/cloud-search/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/cloud-search/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/cloud-search/)
 * [Active Topics](https://wordpress.org/support/plugin/cloud-search/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/cloud-search/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/cloud-search/reviews/)

 * 7 replies
 * 2 participants
 * Last reply from: [Andrea Landonio](https://wordpress.org/support/users/lando1982/)
 * Last activity: [7 years, 6 months ago](https://wordpress.org/support/topic/feature-request-make-custom-fields-sortable/#post-10864096)
 * Status: resolved