Skip Navigation

[Resolved] Sorting View based on two fields

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 8 replies, has 3 voices.

Last updated by Beda 5 years, 5 months ago.

Assigned support staff: Beda.

Author
Posts
#351652

I'm looking for a solution to have a order by multiple cust fields:
1) content-type
2) content-platfom
3) content-lob
4) title

The are multiple views where I would like to apply this order-by clause: 2568, 1900, 1893

I have seen this solution in one of the support threads, but I would not exactly know how to change that in the correct way. (https://toolset.com/forums/topic/order-by-two-or-multiple-parameters/ - which was done by Luoy

//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;
}

Could you provide me the solution to get the requested order by in a function?

#351848

Thank you for contacting us here in the Support Forum

By what sequence you want to Sort By?

If it's the sequence you mention in your post:

1) content-type
2) content-platfom
3) content-lob
4) title

Then you can try this:

add_filter('parse_query', 'add_custom_fiels_to_faq_query');
function add_custom_fiels_to_faq_query($wp_query) {
   if (in_array('post', (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;
}
function custom_order($orderby) {
   global $wpdb;
   return $wpdb->postmeta.'.meta_value, mt1.meta_value, post_date ASC';

Replace "post" with your Custom Post Type Slug.

You will need to add the additional Custom fields as in the example

Please let me know if the above solution works for you, I look forward to your reply!

Thank you for your patience.

#351945

Beda,

The Sort-By is correct.

Then you instructed my to "Replace "post" with your Custom Post Type Slug". But the view is related to Portfolio, Page and Post items. I rather would like to use this for limited set of Views.

In addition, I would like to assign this alternative order-by clause only to a fixed set of views, with the following ID's: 2568, 1900, 1893

I have adjusted the code - as far as possible.

add_filter('parse_query', 'add_custom_fiels_to_faq_query');
function add_custom_fiels_to_faq_query($wp_query) {
   if (in_array('post', (array)$wp_query->get('post_type'))) {
       $meta_query = array();
       $meta_query[] = array('key' => 'wpcf-content-type');
       $meta_query[] = array('key' => 'wpcf-content-platform');
       $meta_query[] = array('key' => 'wpcf-content-lob');
        $meta_query[] = array('key' => 'title');
       $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, mt1.meta_value, post_date ASC';

Is the "build" of the $meta-query[] correct?
And I have some doubts about the last sentence, where "mt1.meta_value, " is mentioned. No Clue where that variable belongs to.

#352258

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Beda is on holiday today. He will be back by tomorrow and he will try to get in touch with you as soon as possible. This is Minesh here to help you further, hope this is OK.

In addition, I would like to assign this alternative order-by clause only to a fixed set of views, with the following ID's: 2568, 1900, 1893
=> To apply ordering with specific view IDs you should try to check your view ID with IF condition using global $WP_Views variable.

I've also modified your code to adjust the title field sorting.

add_filter('parse_query', 'add_custom_fiels_to_faq_query');
function add_custom_fiels_to_faq_query($wp_query) {

global $WP_Views;
$view_ids = array(2568, 1900, 1893);    // add as many view ids you want to include in this array

   if (in_array('post', (array)$wp_query->get('post_type'))   and  

in_array($WP_Views->current_view, $view_ids)) {
       $meta_query = array();
       $meta_query[] = array('key' => 'wpcf-content-type');
       $meta_query[] = array('key' => 'wpcf-content-platform');
       $meta_query[] = array('key' => 'wpcf-content-lob');
        $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 ASC, mt1.meta_value ASC, mt2.meta_value ASC, post_title ASC';
 }

And I have some doubts about the last sentence, where "mt1.meta_value, " is mentioned. No Clue where that variable belongs to.
=> The first function is for getting the custom fields into the query, and the second one sets the order. You can install the "Debug Queries" plugin to see what the code does to the SQL. mt1 and mt2 are alias used for meta_value fields.
https://wordpress.org/plugins/debug-queries/

#352282

Do I need the adjust the shortcode for this views in my pages or can they stay as they are?

[wpv-view name="list-articles-shortcode-typeplatformlob-nl" orderby="title"  order="desc" wpvcontenttype="20,30,31,42,50" wpvcontentplatform="30" wpcontentlob="50"]
#352296

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should keep as it is as 'parse_query' will override the settings.

#352318

When I active the function in my function.php - I do see that the filtering is not working anymore - I do get a full list of items

#352575

I apologize the delay here

This is because the aliases are most probably not correct in your Site/Code.

The line:

  return $wpdb->postmeta.'.meta_value ASC, mt1.meta_value ASC, mt2.meta_value ASC, post_title ASC';

Does set the order-by and sequence of it, but it uses Aliases for your Custom Fields (The WP_Query does that actually, it is not Toolset)

So to have the correct Aliases, you need to var_dump the value of those, so just above place an var_dump($wp_query):

var_dump($wp_query);
return $wp_query;

Then you will see the alias of the fields.

Please if you need help with this, provide me temporary access (WP-Admin and FTP) to your site
- preferably to a test site where the problem has been replicated if possible -
in order to be of better help and check if some configurations might need to be changed

Your next answer will be private which means only you and I have access to it.

❌ Please backup your database and website ❌

✙ I would, if possible, need access to a site where only a minimal set of Plugins and a default theme is active.
This to avoid eventual compatibility issues with other software.

✙ Please add the Links to:

- The Views Edit Screen

- The Page/Post where you insert the View

- The corresponding Front End Page/Screen

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#356688

I apologize the long delay here.

Please do not hesitate to get back at us anytime you will find more time for the project.

Thank you for your cooperation.

P.S.:
I will enable a private answer so your next reply would be hidden too.

Thank you