Skip Navigation

[Resolved] Order by date and custom field

This support ticket is created 3 years, 11 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
- 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 5 replies, has 2 voices.

Last updated by davideE-4 3 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1918637

Hi, it is possibile order view to only years date post and then a custom field?

Example at this link hidden link

#1920237

Minesh
Supporter

Languages: English (English )

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

Jamal passed this ticket to me and I'll take care of this ticket. Hope this is OK.

Using View's GUI, yon can use the custom fields for primary order and post fields for secondary order. With secondary order custom fields are not supported at the moment. We do have feature request for this and we will try to add this feature in near future.

So, as I understand as a primary order, you want to use post_date and as secondary order you want to use your custom field. If this is correct - what if you try to use the view's filter wpv_filter_query and try to pass to multiple orderby prams on fly.

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

You can setup "orderby" parameter with multiple multiple custom field values, see the example of WordPress document:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

Here is the reference ticket that may help you:
=> https://toolset.com/forums/topic/need-help-with-a-secondary-orderby-on-a-view/page/2/#post-419201

#1922459

Hi, i tried this but when i change page the filter doesn't works

function custom_sort($query_args, $views_settings, $view_id) {
  
    if ('6719' === $view_id) {
        $query_args['meta_query'] = array(
            'relation'  => 'AND',
            'wpcf-numero'    => array(
                'key'     => 'wpcf-numero',
                'type'    => 'NUMBER',
                'compare' => 'EXISTS',
            )
		);
  
        $query_args['orderby'] = array(
			'date'       => 'DESC',
            'wpcf-numero'       => 'DESC',
            );
    }
  
    return $query_args;
}
add_filter( 'wpv_filter_query', 'custom_sort', 101, 3);
#1922693

Minesh
Supporter

Languages: English (English )

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

Can you please share problem URL and admin access details.

*** 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.

#1927707

Minesh
Supporter

Languages: English (English )

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

I've adjusted the code you added to your theme's functions.php file as given under:

function func_custom_sort($query_args, $views_settings, $view_id) {
  
    if (6719 == $view_id) {
        
	

		$meta_query = array();
		$meta_query = array(
            'relation'  => 'AND',
            'circolare_q'    => array(
                'key'     => 'wpcf-numero-circolare',
                'type'    => 'NUMBER',
                'compare' => 'EXISTS',
            )
		);
		

		$query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
 
        $query_args['orderby'] = array(
			'post_date'       			=> 'DESC',
            'wpcf-numero-circolare' => 'DESC',
            );
		
				
  
    }
  
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_custom_sort', 101, 3);

Can you please confirm it works as expected.

#1927727

My issue is resolved now. Thank you!