Skip Navigation

[Resolved] Implement second sort to 2 views

This support ticket is created 4 years 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by Dan 4 years ago.

Assisted by: Minesh.

Author
Posts
#2125897

Dan

Hi Minesh,

Following up with my previous ticket from last week (#2125895 - How to display CP in a view only if they're not being displayed in another view) I need your support again, if possible.

For the same views "Top News" and "Featured News", I now need to add a second sort order. Right now it is set by publishing date and the second sort should be by the custom field "Article Placement' where Top news has its own field slug "article-placement-top-news" and Featured news with the slug "article-placement-featured-news" (both numeric fields). So I need the second sort by these fields per each view Ascending.

I believe this is done with yet more custom code (snippets) based on some other support posts I found but I'm not certain how to go about it and if this can be combined with your code snippet.

Kindly let me know.

Thanks,

Dan

#2126255

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Using the "Order" section of the view, you can set the primary and secondary sorting.

But you can set secondary sorting for only default post fields, custom fields are not allowed there, you can say its limitation.

#2126295

Dan

Thanks for the reply, and I might've misunderstood due to my lack of knowledge coding filters. Isn't this possible with what you mentioned here?: https://toolset.com/forums/topic/order-by-date-and-custom-field/ or I'm my missing something.

#2126371

Minesh
Supporter

Languages: English (English )

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

Ok - if you want to use the custom code with filter its possible.

if you do not know how to do it, please let me know and I'm happy to adjust the code for both top and featured news views.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request 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.

I have set the next reply to private which means only you and I have access to it.

#2126613

Minesh
Supporter

Languages: English (English )

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

Can you please check now.

I've added the following code to "Custom Code" section:

function func_order_top_featured_news( $query_args ,$view_settings, $view_id ) {
     
 
    if ( $view_id == 215917 ) {
        
      $add_meta_query = array();
        $add_meta_query = array(
            'relation'  => 'AND',
            '0'    => array(
                'key'     => 'wpcf-article-placement-top-news',
                'type'    => 'NUMBER',
                'compare' => 'EXISTS',
            )
        );
         
 
        $query_args['meta_query'] = $add_meta_query;
      // top news
      $query_args['orderby'] = array(
            'post_date'                 => 'DESC',
            'wpcf-article-placement-top-news' => 'ASC',
            );
     
      
       
    }
  // featured news
  if ($view_id == 215924 ) {
    
      
      $add_meta_query = array();
        $add_meta_query = array(
            'relation'  => 'AND',
            '0'    => array(
                'key'     => 'wpcf-article-placement-featured-news',
                'type'    => 'NUMBER',
                'compare' => 'EXISTS',
            )
        );
         
 
        $query_args['meta_query'] = $add_meta_query;
       
      $query_args['orderby'] = array(
            'post_date'                 => 'DESC',
            'wpcf-article-placement-featured-news' => 'ASC',
            );
      
       
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_order_top_featured_news', 10, 3);

Now, you will have to add the proper order value to the ordering field "article-placement-featured-news" and "article-placement-top-news" and only those posts will be displayed.

#2127351

Dan
Screen Shot 2021-07-27 at 7.07.13 PM.png
Screen Shot 2021-07-27 at 7.12.41 PM.png
Screen Shot 2021-07-27 at 7.12.13 PM.png

Thank you for the additional code Minesh. I truly appreciate it.

I'm still having an issue with the display order at this page: hidden link

If you notice the first 4 articles under Top News view, you'll notice that the order should be different where the second article should be above the first one. This based on the sort order for both custom fields. Please see the attached images. If you look at the admin capture, I added values to the sort ordering and the numbering is correct but not reflecting at the view. ALl articles where published at the same time and same date, so the sort order should be the dictating factor.

I'm also getting a debug pop up, so I'm not sure if this has to do with me changing the sort order for the Top News and Feature News views, as I had them "Ascending" by mistake.

Since I have to work on the site and the debug pop up keeps coming up, I had to turn it off under settings as I'm not sure when you'll be online, so you might want to turn it on.

Thanks again for your time,

Dan

#2127487

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've adjusted the code added to "Custom Code" section as given under:

function func_order_top_featured_news( $query_args ,$view_settings, $view_id ) {
     
 
    if ( $view_id == 215917 ) {
        
      $add_meta_query = array();
        $add_meta_query = array(
            'relation'  => 'AND',
            '0'    => array(
                'key'     => 'wpcf-article-placement-top-news',
                'type'    => 'numeric',
                'compare' => 'EXISTS',
            )
        );
         
 
        $query_args['meta_query'] = $add_meta_query;
      // top news
      $query_args['orderby'] = array(
            'post_date'                 => 'DESC',
            'wpcf-article-placement-top-news' => 'ASC',
            );
     
      
       
    }
  // featured news
  if ($view_id == 215924 ) {
    
      
      $add_meta_query = array();
        $add_meta_query = array(
            'relation'  => 'AND',
            '0'    => array(
                'key'     => 'wpcf-article-placement-featured-news',
                'type'    => 'numeric',
                'compare' => 'EXISTS',
            )
        );
         
 
        $query_args['meta_query'] = $add_meta_query;
       
      $query_args['orderby'] = array(
            'post_date'                 => 'DESC',
            'wpcf-article-placement-featured-news' => 'ASC',
            );
      
       
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_order_top_featured_news', 999, 3);

Can you please confirm it works as expected now.

#2128451

Dan

This works as needed. Sorting by date then by custom field.

Thank you Minesh for your support and patience. Top-notch service from you as always.