Skip Navigation

[Resolved] How to order this way a slider built with Classic View

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by Nigel 5 months, 2 weeks ago.

Assisted by: Nigel.

Author
Posts
#2701004
Screenshot 2024-06-04 at 15.11.06.png
Screenshot 2024-06-04 at 15.11.14.png
Screenshot 2024-06-04 at 15.10.36.png

I have a slider built with a classic view that is used in different posts, to show different related product (that are custom posts).

Inside each post, I have a custom field where the view is added, listing the ids of the products that should go on each page.

Now the order would be different on each page, and the order should follow the ids as they are listed within the shortcode, i.e.:

[wpv-view name="carusel-productos-relacionados-blog" ids="6545,184,6478"]

How can I establish the correct order? I have tried all the available ones but they don't apply. I wish there was a way to add a parameter to the shortcode (or to the view) that allows me to do that...

#2701192

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

A View to query posts uses the core WordPress query functions (e.g. get_posts, which utilises the WP_Query class), and Views provides a UI to specify the query arguments. The most common scenarios are provided for in the UI, but some possible settings may be missing, and you can use the Views API hooks to specify them via code.

If you are passing a list of IDs for the posts that should be returned by the query then I expect that means the query uses the 'post__in' argument to list the IDs.

In which case the 'orderby' argument should be 'post__in', so that it uses that same list to order the results.

See https://developer.wordpress.org/reference/classes/WP_Query/#order-orderby-parameters

You can use the Views API hook wpv_filter_query to override the order by setting from the UI.

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

This request is fairly simple, so let me provide you with an example of the code you would need. Add this as a snippet to Toolset > Settings > Custom Code, the only thing you should need to edit is the View ID you want to apply this to:

function tssupp_filter_query( $view_args, $view_settings, $view_id )  {
  
  // only apply to specific Views
  if ( in_array( $view_id, array( 123 ) ) ) { // Edit with View ID(s)
    
    $view_args['orderby'] = 'post__in';
  }
  
  return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_filter_query', 101, 3 );
#2701212

Hi Nigel, thank you for your support.

Now it's working, thank you!

How can I reuse the same code for a different view?

I tried duplicating the code and just changing the view id but the web crashes. Shall I add the second id to the same code, separated by a comma?

if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12747,12722) ) {

#2701215

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The site is likely crashing because you cannot declare the same function twice.

If you want the same to work with different Views you need to pass the View IDs as an array, so modifying my earlier example to include several Views would give

function tssupp_filter_query( $view_args, $view_settings, $view_id )  {
   
  // only apply to specific Views
  if ( in_array( $view_id, array( 123, 456, 789 ) ) ) { // Edit with View ID(s)
     
    $view_args['orderby'] = 'post__in';
  }
   
  return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_filter_query', 101, 3 );

You shouldn't need to test for is_admin etc. if you add your code to Toolset > Settings > Custom Code, where you can specify where the code should run (e.g. front end).

#2701229

All clear, thank you very much Nigel!