Skip Navigation

[Résolu] how can i orderby custom post_types in views?

This support ticket is created Il y a 7 années et 5 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

Marqué : 

This topic contains 5 réponses, has 2 voix.

Last updated by Adnan Il y a 7 années et 5 mois.

Assisted by: Minesh.

Auteur
Publications
#446074

I am trying to: order the view by custom post_type
like:
post_type_1
post_type_1
post_type_1
post_type_2
post_type_2
post_type_2
post_type_3
post_type_3
post_type_3
post_type_3

without creating a special types field for the order issue.

thank you and kind regards

I visited this URL:

I expected to see:

Instead, I got:

#446176

Minesh
Supporter

Languages: Anglais (English )

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

Hello. Thank you for contacting the Toolset support.

As I understand you would like to display view's results group by post type. If this is correct:

You need to use different method, you need to create new view per post type:

For example:

post type 1:
[wpv-view name="post-type-1"]
post type 2:
[wpv-view name="post-type-2"]
post type 3:
[wpv-view name="post-type-3"]

I hope above solution will help you to resolve your issue.

#446185

hi minesh,
thank you. this is the simplest solution 🙂 i know. but what if i have more then one page or if i use infinite scrolling. i know i need a function but i dont know how.
thank you again
adnan

#446189

for the search i use this one and this works for the search results

add_filter('posts_orderby', 'group_by_post_type', 10, 2);
function group_by_post_type($orderby, $query) {
global $wpdb;
if ($query->is_search) {
return $wpdb->posts . '.post_type ASC';
}
// provide a default fallback return if the above condition is not true
return $orderby;
}

#446254

Minesh
Supporter

Languages: Anglais (English )

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

Well - each view has their own pagination and settings so you can not combine view's settings and results when you are using different views.

You can alter the view's query on fly using the filter: "wpv_filter_query"
=> https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/

You can add posts_orderby filter as given under:

add_filter('wpv_filter_query', 'func_wpv_filter_query', 10, 3);
function func_wpv_filter_query( $query_args, $view_settings, $view_id ) {
        // adjust this condition to decide when to apply the filter
        if ($view_id  == 9999) {  
                add_filter('posts_orderby', 'add_custom_order');
        }
        return $query_args;
}
function add_custom_order($order) {
         global $wpdb;
          return $wpdb->posts . '.post_type ASC';
}

Where:
Replace '9999' with your view ID.

You can adjust your code as needed and please note that this is a custom programming and that is out of the scope of our support policy. If you need custom programming for your site, Please feel free to contact us our certified partners:
=> https://toolset.com/consultant/

#446276

hi minesh

So cool, thank you. works geath. thank you again and kind regards
adnan

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