• Hi! I have added a custom text field using this guide called “order”. I then have written numbers in the order field “1”, “2” etc. and used this guide to attempt to order them by the number in the order field.

    It’s not working as the stores are not in order by the numbers. Am I doing something wrong or should I have done something differently? thanks!

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi there,

    Thanks for reaching out.

    It is hard to tell if you have done anything wrong, I would need to take a look at the implementation in your functions.php file.

    If you don’t want to share those details publicly, you can always open a private support ticket with us ??

    Regards,

    Thread Starter theas91

    (@theas91)

    Hi! Here is the code i have added to functions.php

    
    /* ADD TRENINGSROM AND ORDER TO STORE */
    
    add_filter( 'wpsl_meta_box_fields', 'custom_meta_box_fields' );
    
    function custom_meta_box_fields( $meta_fields ) {
        
        $meta_fields[__( 'Additional Information', 'wpsl' )] = array(
            'phone' => array(
                'label' => __( 'Tel', 'wpsl' )
            ),
            'fax' => array(
                'label' => __( 'Fax', 'wpsl' )
            ),
            'email' => array(
                'label' => __( 'Email', 'wpsl' )
            ),
            'url' => array(
                'label' => __( 'Url', 'wpsl' )
            ),
            'treningsrom' => array(
                'label' => __( 'treningsrom', 'wpsl' )
    		),
            'order' => array(
                'label' => __( 'order', 'wpsl' )
            )
        );
    
        return $meta_fields;
    }
    
    
    add_filter( 'wpsl_frontend_meta_fields', 'custom_frontend_meta_fields' );
    
    function custom_frontend_meta_fields( $store_fields ) {
    
        $store_fields['wpsl_treningsrom'] = array( 
            'name' => 'treningsrom',
            'type' => 'text'
        );
    
    	    $store_fields['wpsl_order'] = array( 
            'name' => 'order',
            'type' => 'text'
        );
    	
        return $store_fields;
    }
    
    
    /* ADD ORDER TO SORTING*/
    
    add_filter( 'wpsl_store_data', 'custom_result_sort' );
    
    function custom_result_sort( $store_meta ) {
        
        $custom_sort = array();
        
        foreach ( $store_meta as $key => $row ) {
            $custom_sort[$key] = $row['order'];
        }
    
        array_multisort( $custom_sort, SORT_ASC, SORT_NATURAL|SORT_FLAG_CASE, $store_meta );
        
        return $store_meta;
    }
    

    Hi again,

    Thanks for sending the code.

    I have tried in my test website and it works. But there is something maybe you didn’t realize. The code above will work only when you do searches in the store locator. The initial map load does not take into account the wpsl_store_data filter. Do you want the order to also affect the initial map load? Let us know.

    Thread Starter theas91

    (@theas91)

    Aha, that is probably what I am doing wrong then, I want to also affect the initial load ??

    Well, in that case you will have to take a look at the wpsl_sql filter as I mentioned.

    I haven’t tried this myself, but it could be something like:

    add_filter( 'wpsl_sql', 'custom_sql' );
    
    function custom_sql() {
        
        global $wpdb, $wpsl_settings;
        
        // Set a limit for the results and order them by distance.    
        if ( isset( $_GET['autoload'] ) && $_GET['autoload'] ) {
            $limit = '';
    
            if ( $wpsl_settings['autoload_limit'] ) {
                $limit = 'LIMIT %d';
            }
    
            $sql_sort = 'ORDER BY sort_order ASC '. $limit;
        } else {
            $sql_sort = 'HAVING distance < %d ORDER BY distance DESC LIMIT 0, %d';
        }
        
        // The default query that selects store location that fall within the selected radius. 
        $sql = "SELECT post_lat.meta_value AS lat,
                       post_lng.meta_value AS lng,
                       post_order.meta_value AS sort_order,
                       posts.ID, 
                       ( %d * acos( cos( radians( %s ) ) * cos( radians( post_lat.meta_value ) ) * cos( radians( post_lng.meta_value ) - radians( %s ) ) + sin( radians( %s ) ) * sin( radians( post_lat.meta_value ) ) ) ) 
                    AS distance        
                  FROM $wpdb->posts AS posts 
            INNER JOIN $wpdb->postmeta AS post_lat ON post_lat.post_id = posts.ID AND post_lat.meta_key = 'wpsl_lat'
            INNER JOIN $wpdb->postmeta AS post_lng ON post_lng.post_id = posts.ID AND post_lng.meta_key = 'wpsl_lng'
            INNER JOIN $wpdb->postmeta AS post_order ON post_order.post_id = posts.ID AND post_order.meta_key = 'wpsl_order'
                 WHERE posts.post_type = 'wpsl_stores' AND posts.post_status = 'publish' $sql_sort";    
    
        return $sql;
    }
    

    Regards ??

    Thread Starter theas91

    (@theas91)

    Thank you! I didn’t add the new code but now it is suddenly in the right order ?? so I guess it worked

    Ok good to know, let us know if you need more help in the future ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Change order by custom field’ is closed to new replies.