Skip Navigation

[Resolved] Sort order set by url parameter

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by nathanW-4 5 years, 4 months ago.

Assigned support staff: Waqas.

Author
Posts
#297001

Hi,

I've used the shortcode provided by Caridad here: https://toolset.com/forums/topic/sorting-a-view-by-a-parameter-based-on-a-custom-field-value/#post-28762

Works well for post title, but I want to use it for date as well and it doesn't seem to be working?

So if i have the url parameter wpv-sort=date-asc set it should re-order by post date descending. It works perfectly for post title but doesn't work for post date. Any ideas why this would be?

Here's my code:


if(!empty($_GET["wpv-sort"])){
    
     add_filter('posts_orderby', 'my_client_order', 98, 2);

    function my_client_order($order, $q) {
    
     $sort = $_GET["wpv-sort"];
    
    if($sort == 'alpha-asc'){
        return 'CHAR_LENGTH(post_title) ASC';
    }
        
    if($sort == 'alpha-desc'){
        return 'CHAR_LENGTH(post_title) DESC';
    }
        
    if($sort == 'date-asc'){
        return 'CHAR_LENGTH(post_date) ASC';
    }
        
    if($sort == 'date-desc'){
        return 'CHAR_LENGTH(post_date) DESC';
    }
        
           
    }
    
    

}

Thanks

#297419

Any ideas on this?

#297422

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Post Dates are not characters or strings, these are actually timestamps. I suspect the CHAR_LENGTH() may not work in this case. Please try following change for date-asc and date-desc:

    if($sort == 'date-asc'){
        return 'post_date ASC';
    }
         
    if($sort == 'date-desc'){
        return 'post_date DESC';
    }

Please let me know if it works, or if I can help you with anything related.

#301969

All sorted, thanks