Skip Navigation

[Resolved] Custom order by with 2 custom fields

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 7 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

This topic contains 14 replies, has 3 voices.

Last updated by raulE-2 7 years, 11 months ago.

Assisted by: Ghennadi.

Author
Posts
#387362

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)?

#387594

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

#387628

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?

#387663

Hello.

Fixed Caridad code

add_filter('parse_query', 'add_custom_fiels_to_faq_query', 99, 3);
function add_custom_fiels_to_faq_query($wp_query, $view_settings, $view_id ) {
   if ( $view_id != HEREPLACEVIEWID ){
      return;
   }
   if (in_array('faq', (array)$wp_query->get('post_type'))) {
       $meta_query = array();
       $meta_query[] = array('key' => 'wpcf-custom_field1');
       $meta_query[] = array('key' => 'wpcf-custom_field2');
       $wp_query->set('meta_query', $meta_query);
       add_filter('posts_orderby', 'custom_order');
   }
   return $wp_query;
}
#387671

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?

#387698

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';
}
#387702

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?

#387706

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.

#387731

Hello

Please try this version:

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');
       if ( !isset($query_args['meta_query']) ){
       		$query_args['meta_query'] = array();
	   }
       $query_args['meta_query'] = array_merge($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';
}
#387735

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.

#387775

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';
}
#387780

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.

#387789

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!

#387820

Hello

Please back that code and replace '34' to your view id. Otherwise this will applied to all your views.

#524248

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 🙁

The forum ‘Types Community Support’ is closed to new topics and replies.

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