Skip Navigation

[Resolved] View order setting…

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 10 replies, has 3 voices.

Last updated by Waqar 8 months, 2 weeks ago.

Assisted by: Waqar.

Author
Posts
#2692964
Screenshot 2024-04-18 at 12.25.37.png
CorrectOrder.png

We have a slider built using Toolset Views.
There is a custom field repeat group on each product page where we decide which products must go in the slider.

I would like to have the slider order to follow the same order those products have on the custom field group, but I cannot find a way to do it.

This is an example page: hidden link - the slider it's at the bottom of the page.
I attach an image of what should be the correct order and of the view's order settings.

What do I have to do to achieve the correct order?

Thank you in advance

#2693066

Nigel
Supporter

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

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

Hi there

I'm not sure, because I'm having to make some guesses about how I think you have this set up.

But given that you are passing a list of post IDs to include via the ids shortcode attribute, that would imply that the resulting query to retrieve the posts uses the 'post__in' argument to specify those post IDs.

(You could double-check this by enabling the Views debug mode at Toolset > Settings > Front-end Content and then loading the page you linked to on the front end. You'll get a pop-up with some debug info, including details of the query arguments from wpv_filter_query.)

If that is the case then you could set the query argument 'orderby' to 'post__in' so that they appear in the same order as the list you pass.

(See https://developer.wordpress.org/reference/classes/WP_Query/parse_query/.)

You cannot choose that from the UI, though (it's a somewhat unexpected use case).

So you would need to use the filter wpv_filter_query to overwrite the setting for 'orderby' specified in the UI.

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

#2693182

Thank you Nigel.

I think the query does use the post_in argument.

I did not understand how I should add the wpv_filter_query query though. Is it something I should add in my functions.php?

SELECT   wp_posts.*
			 FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) LEFT  JOIN wp_icl_translations wpml_translations
							ON wp_posts.ID = wpml_translations.element_id
								AND wpml_translations.element_type = CONCAT('post_', wp_posts.post_type) 
			 WHERE 1=1  AND wp_posts.ID IN (7275,7714,7715,7716,7717,7718,7719,7720,7721,7722,7723,7724,7725,7726,7727,7728,7729,7730,7731,7732) AND ( 
  wp_postmeta.meta_key = 'toolset-post-sortorder'
) AND wp_posts.post_type = 'accesorios-repeat' AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')) AND ( ( ( wpml_translations.language_code = 'es' OR (
					wpml_translations.language_code = 'es'
					AND wp_posts.post_type IN ( 'post','gp_elements','sp_post_carousel','view-template','slider-home','analisis-alimentario','bloques-home','sector-principal','faq-vertical-topline','nuestra-historia','accesorios-repeat','accesorios-food-rpt','app-repeat','app-repeatable-pm','app-food','faq-serie-autoclaves','faq-equipos-food','faq-vertical-classic','faq-sobremesa','faq-preparadores','faq-medicas','faq-digestor-kjedahl','faq-scrubber','faq-dest-kjedahl','faq-sx6','faq-extractor-fibras','faq-enodest','faq-extractor-grasas','faq-servicio-tecnico','id-post-group','autoclaves','wpa-helper' )
					AND ( ( 
			( SELECT COUNT(element_id)
			  FROM wp_icl_translations
			  WHERE trid = wpml_translations.trid
			  AND language_code = 'es'
			) = 0
			 ) OR ( 
			( SELECT COUNT(element_id)
				FROM wp_icl_translations t2
				JOIN wp_posts p ON p.id = t2.element_id
				WHERE t2.trid = wpml_translations.trid
				AND t2.language_code = 'es'
                AND (
                    p.post_status = 'publish' OR p.post_status = 'private' OR 
                    ( p.post_type='attachment' AND p.post_status = 'inherit' )
                )
			) = 0 ) ) 
				) ) AND wp_posts.post_type  IN ('post','page','attachment','wp_block','wp_template','wp_template_part','wp_navigation','gp_elements','sp_post_carousel','view-template','slider-home','analisis-alimentario','bloques-home','sector-principal','faq-vertical-topline','nuestra-historia','accesorios-repeat','accesorios-food-rpt','app-repeat','app-repeatable-pm','app-food','faq-serie-autoclaves','faq-equipos-food','faq-vertical-classic','faq-sobremesa','faq-preparadores','faq-medicas','faq-digestor-kjedahl','faq-scrubber','faq-dest-kjedahl','faq-sx6','faq-extractor-fibras','faq-enodest','faq-extractor-grasas','faq-servicio-tecnico','id-post-group','autoclaves','wpa-helper' )  ) OR wp_posts.post_type  NOT  IN ('post','page','attachment','wp_block','wp_template','wp_template_part','wp_navigation','gp_elements','sp_post_carousel','view-template','slider-home','analisis-alimentario','bloques-home','sector-principal','faq-vertical-topline','nuestra-historia','accesorios-repeat','accesorios-food-rpt','app-repeat','app-repeatable-pm','app-food','faq-serie-autoclaves','faq-equipos-food','faq-vertical-classic','faq-sobremesa','faq-preparadores','faq-medicas','faq-digestor-kjedahl','faq-scrubber','faq-dest-kjedahl','faq-sx6','faq-extractor-fibras','faq-enodest','faq-extractor-grasas','faq-servicio-tecnico','id-post-group','autoclaves','wpa-helper' )  )
			 GROUP BY wp_posts.ID
			 ORDER BY wp_postmeta.meta_value+0 ASC
#2693234

Hi,

Thank you for confirming this.

Here is an example of a custom function that you can use with the 'wpv_filter_query' filter:


add_filter( 'wpv_filter_query', 'filter_posts_order_fn', 1000 , 3 );
function filter_posts_order_fn( $query_args, $view_settings ) {
	// check is a specific form
    if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
		$query_args['orderby'] = 'post__in';        
	}
	return $query_args;
}

Note: You'll replace '12345' with the actual ID of your target view.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

regards,
Waqar

#2693240

Thank you very much Waqar: I am almost there... Is there a way to configure the DESC / ASC parameter?

The products are showing in the opposite order, I would like to change it from DESC to ASC.
I have tried adding the "order" parameter but I am doing something wrong ...

#2693242

Glad it worked.

You can include the 'order' attribute in the function like this:
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters


add_filter( 'wpv_filter_query', 'filter_posts_order_fn', 1000 , 3 );
function filter_posts_order_fn( $query_args, $view_settings ) {
    // check is a specific form
    if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
        $query_args['orderby'] = 'post__in';
        $query_args['order'] = 'ASC';     
    }
    return $query_args;
}

#2693249

Thank you Waqar,
I was doing it right then.

The problem is, the order parameter doens't change the results: the order is always the same.

What's wrong then?

#2693259

Some third-party plugins can override the WordPress queries, globally, including the ones used by the views.

Can you please test this with all non-Toolset plugins disabled and with a default WordPress theme like Twenty Twenty-One?

#2693547

Hi,
I have tried deactivating all non-Toolset plugins and switch the template to Twenty Twenty-one but the order is still the other way round...

#2693551

To troubleshoot this, I'll need to see how this view is set up in the admin area.

Can you please share temporary admin login details, along with the exact steps to see the issue?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

#2693836

Thank you for sharing the access details.

I noticed that the IDs for the view's shortcode attribute are coming from another view 'get-all-ids-for-slider'.

I changed this view's ordering settings to 'Ascending' from 'Descending' and commented out the line '$query_args['order'] = 'ASC';' from your custom code snippet and the order is inverse now.