alter existing code to sort users profiles according to hourly rate in decreasing order

This function can sort profiles of users according to hourly rate (increasing order), or to date joined (newest first). How can I alter this function such that hourly rate is in decreasing order?

function:

function pre_get_profile($query)   {

        if( is_post_type_archive('fre_profile') ) {

            $query_profile  = $query->query;
            $post_type      = isset( $query_profile['post_type'] ) ? $query_profile['post_type'] :'';

            if( $post_type == PROFILE ) {

                if( is_admin() ) {
                    /*
                     * @since version 1.7.9
                     * Fix dashboard profiles menu tabs(list profiles is not enough).
                    */
                    return $query;
                }

                $query->query_vars['meta_query'] = '';
                if( isset( $_REQUEST['query']['hour_rate'] ) && !empty( $_REQUEST['query']['hour_rate'] ) ) {

                    $hour_rate = $_REQUEST['query']['hour_rate'];
                    $hour_rate = explode(",",$hour_rate);
                    $query->query_vars['meta_query'][] =  array( 'key'  =>'hour_rate',
                                                            'value'     => array((int)$hour_rate[0], (int) $hour_rate[1]),
                                                            'type'      => 'numeric',
                                                            'compare'   => 'BETWEEN'
                                                    );
                } else {
                    // Query Hour_rate default
                    $query->query_vars['meta_query'][] =  array( 'key'  =>'hour_rate',
                                                            'value'     => array(0,(int)ae_get_option('fre_slide_max_budget_freelancer', 2000)),
                                                            'type'      => 'numeric',
                                                            'compare'   => 'BETWEEN'
                                                    );
                }

                if( ! current_user_can( 'manage_options' ) ) {
                    /*
                     * fre/emp/visitor only see profile is available for hire.
                     */

                    $query->query_vars['meta_query'][] = array( 'key'   =>'user_available',
                                                            'value'     => 'on',
                                                            'compare'   => '='
                                                    );
                }
            }

        }
        // Search default
        if ( $query->is_search() && is_search() && !is_admin()) {
            $query->set( 'post_type', array('post','page') );
        } // end if

        return $query;
    }
  • Adam Czajczyk

    Hello Layal,

    I hope you're well today and thank you for your question!

    Can you tell me please what does this code is related to - would that be some theme or plugin? I do not know what the data structure is and what that data comes from so that code is a bit "out of context" and knowing where does it come from could help with getting to know about data and how to sort it.

    Let me know please.

    Best regards,
    Adam

  • Layal

    Sorry for lack of details.. this function is inside a file named profiles.php which contains functions related to users profiles. This file is present inside the themes files/includes/
    SO on the homepage of bioinfexperts.com, you can see the list of profiles of users. The list can be sorted in two ways as I mentioned before. I asked the theme developers how can sort them according to decreasing hourly rate, and they just pointed me to this function without any additional info. I tried to figure out how to do this, but I failed.

  • Predrag Dubajic

    Hi Layal,

    I've added 'order' => 'DESC' below 'compare' => 'BETWEEN' lines so could you try replacing the code with the below one and see if that does the trick:

    function pre_get_profile($query)   {
    
            if( is_post_type_archive('fre_profile') ) {
    
                $query_profile  = $query->query;
                $post_type      = isset( $query_profile['post_type'] ) ? $query_profile['post_type'] :'';
    
                if( $post_type == PROFILE ) {
    
                    if( is_admin() ) {
                        /*
                         * @since version 1.7.9
                         * Fix dashboard profiles menu tabs(list profiles is not enough).
                        */
                        return $query;
                    }
    
                    $query->query_vars['meta_query'] = '';
                    if( isset( $_REQUEST['query']['hour_rate'] ) && !empty( $_REQUEST['query']['hour_rate'] ) ) {
    
                        $hour_rate = $_REQUEST['query']['hour_rate'];
                        $hour_rate = explode(",",$hour_rate);
                        $query->query_vars['meta_query'][] =  array( 'key'  =>'hour_rate',
                                                                'value'     => array((int)$hour_rate[0], (int) $hour_rate[1]),
                                                                'type'      => 'numeric',
                                                                'compare'   => 'BETWEEN',
                                                                'order'   => 'DESC'
                                                        );
                    } else {
                        // Query Hour_rate default
                        $query->query_vars['meta_query'][] =  array( 'key'  =>'hour_rate',
                                                                'value'     => array(0,(int)ae_get_option('fre_slide_max_budget_freelancer', 2000)),
                                                                'type'      => 'numeric',
                                                                'compare'   => 'BETWEEN',
                                                                'order'   => 'DESC',
                                                        );
                    }
    
                    if( ! current_user_can( 'manage_options' ) ) {
                        /*
                         * fre/emp/visitor only see profile is available for hire.
                         */
    
                        $query->query_vars['meta_query'][] = array( 'key'   =>'user_available',
                                                                'value'     => 'on',
                                                                'compare'   => '='
                                                        );
                    }
                }
    
            }
            // Search default
            if ( $query->is_search() && is_search() && !is_admin()) {
                $query->set( 'post_type', array('post','page') );
            } // end if
    
            return $query;
        }

    Let us know how it goes.

    Best regards,
    Predrag

  • Adam Czajczyk

    Hello Layal!

    That is probably because of how the theme structures/handles data. I'm not sure about how this is done "by code" in case of that theme and the theme seems to be a premium one with no free/lite version available so I'm not able to test it on my end.

    I would need to take a look at theme files and the database so please send in:

    Subject: "Attn: Adam Czajczyk"

    - Mark to my attention, the subject line should contain only: ATTN: Adam Czajczyk
    - Do not include anything else in the subject line, doing so may delay our response due to how email filtering works.
    - Link back to this thread
    - login URL and admin account login credentials (may be a temporary admin account) data
    - Include FTP log-in details (hostname, username & password)
    - Include hosting control panel access details (login address, username & password) - cPanel's usually the control panel used for this, but your provider may use something else; I'll need this for accessing your site's database, preferably via phpMyAdmin
    - Include any relevant URLs for your site

    Please use our contact form here https://premium.wpmudev.org/contact/#i-have-a-different-question

    I'll then access your site, review theme files to see how it works and check the database to see how data is structured and I hope I'll be able to help then.

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.