Skip Navigation

[Resolved] Sort View output by shortcode parameter (ID)

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 0 replies, has 1 voice.

Last updated by haraldO 1 week, 1 day ago.

Assisted by: Minesh.

Author
Posts
#2803441

Tell us what you are trying to do?
I have a view that is placed on a page via Shortcode like this:
[wpv-view name="View Mitarbeiter Grid by ID" ids="2737,2646,4007,4035,4039,4044,2968"]
I want to order the output by the exact same order I provided in the shortcode.

Is there any documentation that you are following?
Yes, a half dozen posts on the forum and in the documentation ...
https://toolset.com/documentation/legacy-features/views-plugin/filtering-views-query-by-post-id/
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/forums/topic/sort-distance-filter-results-from-closest-to-most-far-away-and-output-distance/
https://toolset.com/errata/cannot-order-results-by-distance-in-views-created-with-the-block-editor/
https://toolset.com/forums/topic/sort-by-view/
and so on

What is the link to your site?
hidden link

I also tried some custom code, but couldn't get it working:
add_filter('wpv_filter_query', function($query_args, $view_settings) {
// Check if it's the correct View
if (isset($view_settings['view_id']) && $view_settings['view_id'] == '17219') {
// Get the IDs from the shortcode
if (isset($view_settings['ids']) && !empty($view_settings['ids'])) {
$ids = explode(',', $view_settings['ids']);
$query_args['post__in'] = $ids; // Include only these posts
$query_args['orderby'] = 'post__in'; // Order by the given IDs
}
}
return $query_args;
}, 101, 2);

#2803624

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL and admin access details and tell me on what section you added your view.

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

#2803799

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

You can use the View's API filter hook wpv_filter_query to change the view's query argument on fly.
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

I've added the following view's filter hook to "Custom Code" section offered by Toolset:
=> hidden link

add_filter('wpv_filter_query','func_change_sort_order_as_per_views_shortcode_argument', 101, 3);
function func_change_sort_order_as_per_views_shortcode_argument($query_args, $view_settings,$view_id) {
    // Check if it's the correct View
    if ($view_id == 17219) {
        
     	global $WP_Views;
      
          $shortcode_attribute_ids = $WP_Views->view_shortcode_attributes[0]['ids'];
          $ids = explode(',', $shortcode_attribute_ids);
            $query_args['post__in'] = $ids; // Include only these posts
            $query_args['orderby'] = 'post__in'; // Order by the given IDs
        
    }
    return $query_args;
}
#2803935

Works perfectly, thanks a lot!