Skip Navigation

[Resolved] Order by two or multiple parameters

This support ticket is created 9 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 13 replies, has 2 voices.

Last updated by Antonio 9 years ago.

Assisted by: Luo Yang.

Author
Posts
#260787

Hi,
I set a view to show posts on homepage filtered by specific category and ordered by date.

What I need is to order posts by two parameters instead of just date. To be more precise, I need them ordered by a custom parameter first and then by date, something like featured first then date.

I set up an "EVIDENZA" checkbox custom field (the one to push posts always on top). So I need the posts marked as EVIDENZA to be on top of the list then 4 more posts ordered by date (5 posts total to be displayed)

I tried using this code

add_filter('wpv_filter_query', 'featured_sorting', 10, 2);
function featured_sorting($q, $view) {
        if ($view['view_id'] == 385) {
                $q['meta_key'] = 'wpcf-evidenza';
                $q['orderby'] = array('meta_value', 'date');
                $q['order'] = 'DESC';
        }
        return $q;
}

Can it be a starting point? How do I set it up to show 5 posts?

Thank you.

#260905

I suggest you try Views filter hook wpv_filter_query to apply your custom order parameters,
More help:
wpv_filter_query
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class.
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/

Order & Orderby Parameters
http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

For example the similar thread:
https://toolset.com/forums/topic/sort-query-by-custom-field-then-title/#post-100568

#260985

Thanks for your suggestions.

I set up my view to order by custom field FIRST then using this piece of code

add_filter('wpv_filter_query', 'setup_order', 10, 2);
function setup_order($q, $view) {
        // adjust this condition to decide when to apply the filter
        if ($view['view_id'] == 385) {  
                add_filter('posts_orderby', 'adjust_order');
        }
        return $q;
}
function adjust_order($order) {
        remove_filter('posts_orderby', 'adjust_order');
        $order .= ", wp_posts.post_date DESC";
}

I tried ordering by date.

Now, posts marked by the custom field display in my view but the order by date does not work. It seems that this line

$order .= ", wp_posts.post_date DESC";

has no effect at all.

#261105

Please try this:

...
global $wpdb;
$order .= ', ' . $wpdb->prefix . 'posts.post_date DESC';
...

More help:
$prefix
The assigned WordPress table prefix for the site.
http://codex.wordpress.org/Class_Reference/wpdb

#261162

Thank you.

The line you provided has no effect, either DESC or ASC my posts are ordered randomly after the first one which has the EVIDENZA custom field.

My custom field is a checkbox but in my view settings I must select descending or ascending order, but for a checkbox it's not important. Could it be the reason why the ordering by date does not work?

Is an "ordering by multiple parameters" function on the roadmap for Views?

#261551

Could you duplicate same problem in a test site, and fill below private detail box with login details,

Also point out the problem view URL and problem page URL, I need a live website to debug this problem, thanks

#262101

To debug this problem, I need the login details and ftp access of your test site, also point out where can I edit your PHP codes. thanks

#262331

Thanks for the details, I am trying to log into your website, and will feedback if there is any found.

#262356

I suggest you try this:
1) modify the problem view "Avvisi home", add a filter:
Select posts with custom field:
wpcf-evidenza = 1

display limit 1 item

2) duplicate above view "Avvisi home" as "Avvisi home not EVIDENZA"
hidden link
filter:
Select posts with custom field:
wpcf-evidenza = 0

display limit 4 item

And put above two views in same place, then it should be able to display as what you need:
"posts marked as EVIDENZA to be on top of the list then 4 more posts ordered by date (5 posts total to be displayed)".

I am not sure how do you output the views, so I did not modify your theme files, just create the second view "Avvisi home not EVIDENZA", for your reference.

#263195

Sorry for not replying earlier, I did not get any email notification.

I'm sorry, but the solution you suggest is a bit odd... I understand what you say, but what if my client needs more than 1 post EVIDENZA? There is no way of knowing it before and I can not modify Views each time he needs.

You could have asked for more info, I have no problem at all as long as I need this problem solved.

I output my Views this way in tmpl_home.php

<?php
        $args = array('name'=>'avvisi-home');
                echo(render_view($args));
?>

Please, ask for more info if you need and feel free to edit my theme files (do a copy before).

Thank you

#263504

The problem view is already been setup as order by "data del post" "Decrescente", I guess it is "post date" desend
So the custom php code won't work, it will follow the order of Views setting,

And I can not find the custom field "wpcf-evidenza" you mentioned in post:
https://toolset.com/forums/topic/order-by-two-or-multiple-parameters/#post-260787

Please check it.

#263510

Thanks for replying.

Of course it is ordered by date/DESC, since I had to revert back to my original ordering because it did not work the way I would need it to.

You can find the field in group EXTRA -> field "IN EVIDENZA" (slug EVIDENZA). I'm using capitals here just to make it easier to you to identify them.

#263775

Thanks for the details, I have add below codes into your theme functions.php:

//Order by two or multiple parameters
add_filter('wpv_filter_query', 'setup_order', 10, 2);
function setup_order($q, $view) {
        // adjust this condition to decide when to apply the filter
        if ($view['view_id'] == 385) {  
                add_filter('posts_orderby', 'adjust_order');
        }
        return $q;
}
function adjust_order($order) {
        remove_filter('posts_orderby', 'adjust_order');
		global $wpdb;
		$order .= ', ' . $wpdb->prefix . 'posts.post_date DESC';
		return $order;
}

Please check again if it is what you need.

#263848

Thanks for replying,
It seems that my problem is solved and now the query behaves the way I need.

Thanks for supporting me.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.