Skip Navigation

[Resolved] Slider order using list of post IDs

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to sort or orderby view's results with view's shortcode attribute (ids)

Solution:
You can use the view's filter hook "wpv_filter_query" to set the orderby query argument on fly.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/slider-order-using-list-of-post-ids/#post-2684415

Relevant Documentation:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

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 1 reply, has 2 voices.

Last updated by Minesh 10 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2684399

I have a slider which takes a shortcode attribute (ids) as the post IDs of slider items. I really want them to appear in the order they are given in the comma delimited string of Post IDs but I can't use random as the sort order for the view because that gives odd results and any other order overrides the order of the post IDs in the string. The string is produced by a custom function which takes certain posts from different post types, randomises them and outputs them to the string.

Is there any way to achieve this and override the view ordering? I don't mind writing code but need some help understanding how to override the view order

#2684415

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

To order the view result based on the shortcode attribute "ids" values, you will have to add the view's filter hook "wpv_filter_query":

You should try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

add_filter( 'wpv_filter_query', 'func_orderby_shortcode_attribute_ids', 101, 3 );
function func_orderby_shortcode_attribute_ids($query, $setting, $views_ID) {
    if($views_ID == 99999) {
         
            $query['orderby'] = 'post__in';
                      
    }
    return $query;
}

Where:
- Replace 99999 with your original view ID.

More Info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#2684584

Thanks so much Minesh. As always from Toolset Support a great response and done in super fast time! This works perfectly.

I really appreciate it.