Hi,
I have 2 custom field:
- a select (that save on db a number from 0 to 9) wpcf-custom1
-a numeric field wpcf-custom2
I would sort my view by custom1(DESC order) than custom2(ASC order).
Can anyone provide me the code?
And how I have to set my view order by (in page "Modify View" and in shortcode)?
Hello
Unfortunately order View by 2 custom fields impossible at the moment. But you can extend View query using this filter : https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/
This article also help you write correct query: hidden link
Hi and thank you for reply,
I use code provide by Caridad in this post: https://toolset.com/forums/topic/multiple-sort-criteria/
It work well but that filter is applied in all view.
How can I modified that code so that this filter is applied only in one view?
This code it doesn't work... It seems that $view_id and my view id are different.
There is a way to show value of $view_id?
Hello
I'm sorry there was wrong filter, please use this code.
Notes:
- replace YOURVIEWID to real view id
- replace wpcf-num1 and wpcf-num2 to your fields name ( do not forget about wpcf- prefix for Types fields)
add_filter('wpv_filter_query', 'add_custom_fiels_to_view_query', 99, 3);
function add_custom_fiels_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id != YOURVIEWID ){
return;
}
$meta_query = array();
$meta_query[] = array('key' => 'wpcf-num1');
$meta_query[] = array('key' => 'wpcf-num2');
$query_args['meta_query'] = $meta_query;
add_filter('posts_orderby', 'custom_order');
return $query_args;
}
function custom_order($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value, mt1.meta_value, post_date ASC';
}
I found another problem that is more important. In page "Modified View", I have set some query filter.
But with orderby function, those filters are not considered. The view show me all custom post types.
How can I resolve that?
Hello.
You filter posts by custom fields in View? Can you please upload screenshot from Views edit page ( filters part )
Note. I will set next reply as private. Only you and me will see that reply.
This code not work for me.
Can you modified that code?
add_filter('parse_query', 'add_custom_fiels_to_faq_query');
function add_custom_fiels_to_faq_query($wp_query) {
if (in_array('faq', (array)$wp_query->get('post_type'))) {
$meta_query = array();
$meta_query[] = array('key' => 'wpcf-field1');
$meta_query[] = array('key' => 'wpcf-field2',);
$wp_query->set('meta_query', $meta_query);
add_filter('posts_orderby', 'custom_order');
}
return $wp_query;
}
function custom_order($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value DESC, mt1.meta_value ASC';
}
Thanks.
Hello.
You can not add that filter for specific View ID, you have to use Views filters.
Also, can you specify what exactly doesn't work?
I found that previous code can sort by incorrect meta fields.
please try this:
add_filter('wpv_filter_query', 'add_custom_fiels_to_view_query', 99, 3);
function add_custom_fiels_to_view_query($query_args, $view_settings, $view_id ) {
if ( $view_id != 34 ){
return;
}
$meta_query = array();
$meta_query[] = array('key' => 'wpcf-num1');
$meta_query[] = array('key' => 'wpcf-num2');
if ( !isset($query_args['meta_query']) ){
$query_args['meta_query'] = array();
}
$query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
add_filter('posts_orderby', 'custom_order');
return $query_args;
}
function custom_order($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value, mt1.meta_value, post_date ASC';
}
That code not work. It display only 6 post types and are blank.
Works fine only with code that I've write in previous reply,... but I have the problem that not considered the other filters.
From your code I delete :
if ( $view_id != 34 ){
return;
}
and now it work!!!
Thank for your patient and sorry for the waste of time!
Have a nice day!
Hello
Please back that code and replace '34' to your view id. Otherwise this will applied to all your views.
Hello
I will need also to order one view from tow different custom fields. Does this simple option still not available in tool-set?
Do I have to work with modified code? I bought Tool-set hoping not to get my hands on php code 🙁