Skip Navigation

[Closed] View including sort based on multiple columns

This support ticket is created 7 years, 7 months 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 7 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#427872

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

I have started this question earlier, but never came to a working solution.
https://toolset.com/forums/topic/sorting-view-based-on-two-fields/

I have added the following code to my functions.php file, based on the earlier discussion, but with no results.

// CREATE SORTING ORDER MULTIPLE
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(2268);    // 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';
 }
#427992

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Stefan

I looked at your earlier post and saw that you had some additional requirements. Can I verify that those are still required before I propose a solution?

You said in that post that "the view is related to Portfolio, Page and Post items. I rather would like to use this for limited set of Views... with the following ID's: 2568, 1900, 1893".

Is that still the case?

#428025

Nigel,

These are indeed two approaches that will help me to get things sorted out based on multiple columns. Burt I hope to get an answer of both questions, as I'm planning to use both of them.
Let's keep them separated.

I would like to see the sorting of multiple columns related to entries of Posts, Pages and Portfolio items and indeed for a selected number of Views where this sorting is applicable.

#428597

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Stefan

I have written a new code sample for you to try. I don't have the same set-up as you with your custom post types and fields etc., so I haven't tested it, but I think it should work.

Notes:
- you should double-check the slugs for your custom fields. Note that they are stored in the wp_postmeta table with a "wpcf-" prefix, so I am using "wpcf-content-type" etc.
- I don't know what your custom field types are. I've set them all as STRING but maybe they need to be changed to, for example, NUMERIC
- take note of the where the orderby query arg is defined. This means the results are ordered by type, then platform, then lob, and then the post title. I don't know whether you want them in ASC or DESC order, you will need to adjust

And note that there is an important limitation here. This alters the query generated by your view. You can use it in a normal view, but if you try to use it on a parametric search then modifying the search filters will interfere with the customisation we have created and it will break.

Here's the code, let me know how you get on with it:

/**
 *  Custom sorting of portfolio, pages, and posts
 */
function views_custom_sort($query_args, $views_settings, $view_id) {
 
    $target_types = array( 'post', 'page', 'portfolio' );
    $target_views = array( '2568', '1900', '1893' );

    if ( in_array( $view_id, $target_views ) ) {
 
        $query_args['post_type'] = $target_types;

        $query_args['meta_query'] = array(

            'relation'  => 'AND',
          
            'type_q'    => array(
                'key'     => 'wpcf-content-type',
                'type'    => 'STRING',
                'compare' => 'EXISTS',
            ),
          
            'platform_q'    => array(
                'key'     => 'wpcf-content-platform',
                'type'    => 'STRING',
                'compare' => 'EXISTS',
            ),

            'lob_q'    => array(
                'key'     => 'wpcf-content-lob',
                'type'    => 'STRING',
                'compare' => 'EXISTS',
            )
        );
 
        $query_args['orderby'] = array(
            'type_q'            => 'ASC',
            'platform_q'        => 'DESC',
            'lob_q'             => 'ASC',
            'title'             => 'DESC'
        );
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'views_custom_sort', 101, 3);

If you want to learn more see this WordPress announcement: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

#429614
Screenshot 2016-08-29 12.42.32.png
Screenshot 2016-08-29 12.18.45.png

Thanks for the update.

I ran into some problems: the function is changing the table layout or it is even not showing any results. It is also asking for a password - very strange.

I have made a slide change to the ordering sequence

$query_args['orderby'] = array(
              'platform_q'        => 'ASC',           
              'type_q'            => 'ASC', 
              'lob_q'             => 'ASC',
              'title'             => 'ASC'

By making this change, a regular view is working fine.
By another view, the layout is still a formatted in the wrong way and there is a popup or a password

#429698

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Stefan

If you are seeing a pop-up asking for a password then it sounds like you have a password-protected post or page or portfolio on your site.

I'm not sure what your Views are set up to display, but with this custom snippet we are telling WP_Query to return all of the posts and all of the pages and all of the portfolio post types on your site, including things like your contact page, your "Hello World!" post if you haven't deleted it etc.

So any one of those that is password-protected would trigger the password prompt.

And they may be interfering with your layout depending on what the View expects to be returned.

Do you really want every post, page, and portfolio on each of those Views?

What are those Views and what content are you querying?

The topic ‘[Closed] View including sort based on multiple columns’ is closed to new replies.