• pixelkicks

    (@pixelkicks)


    Hi,

    I have the following custom function that is trying to modify the ORDER BY query:

    function custom_ajwpsf_ometa_type($ordervalue, $getdata, $id) {
    	if (isset($getdata['sort_filter'])) {
    		switch($getdata['sort_filter']) {
    		case 'A-Z':
    		case 'Z-A':
    			$ordervalue = 'title';
    			break;
    		case 'date':
    			$ordervalue = 'date';
    			break;
    		case 'month':
    			$ordervalue = "meta_value";
    			break;
    		}
    	}
    	else {
    		$ordervalue = "_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0";
    	}
    
    	return $ordervalue;
    }

    The reason for this is that I have a select menu, titled “Sort By”, which contains the following four items:

    – Title (A-Z)
    – Title (Z-A)
    – Date Published
    – Current Events

    In the case statement above, “month” matches the “Current Events” item.

    Now, the two Title fields and the Date published field sort the query perfectly. However, the Current Events (month) field doesn’t work. In the SQL it shows the following column that its trying to sort by.

    wp_postmeta.meta_value

    Now normally this would work perfectly, however the dates in our database are stored in dd/mm/yyyy format, so they don’t sort naturally. In a manual SQL query, we can counter this by using:

    STR_TO_DATE(meta_value, ‘%d/%m/%Y’)

    So my question is, how do I customise the query to do this in the function above?

    Thanks,

    Chris.

    https://www.ads-software.com/plugins/ajax-wp-query-search-filter/

Viewing 1 replies (of 1 total)
  • Plugin Author TC.K

    (@wp_dummy)

    If you want to sort by meta value, you will also need to defined the order ‘meta_key’ key as well.

    In your case, since you combine date, title and meta field, you should use the ajax_wpqsf_query filter instead.

Viewing 1 replies (of 1 total)
  • The topic ‘Modify the "ORDER BY" query’ is closed to new replies.