Skip Navigation

[Resuelto] Filter view by comma separated Ids as url parameters

This support ticket is created hace 3 años, 9 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Etiquetado: ,

Este tema contiene 4 respuestas, tiene 2 mensajes.

Última actualización por hispavista-s.l.E hace 3 años, 8 meses.

Asistido por: Waqar.

Autor
Mensajes
#1966791

Hi,

I would like to create a view with Post ID filter that include only post with Ids determined by URL parameter "ids", but I only get the first one. My url ids are comma separated like this: mysite/page-with-my-view/?ids=190,248,300 and can't change it.
Would you help me to make it work?
Thanks a lot.

#1967761

Hi,

Thank you for contacting us and I'd be happy to assist.

To pass multiple IDs through a URL parameter, you can use the array format like this:

mysite/page-with-my-view/?ids[]=190&ids[]=248&ids[]=300

regards,
Waqar

#1967911

Hi,

thanks for your reply, but I wonder is there's any other way to capture the array without changing the url structure, must maintain the ids separated with commas.

#1967967

To keep using the URL structure with comma-separated IDs, you'll need to override the query using the "wpv_filter_query" filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Example:


add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
	// skip if blocks edit screen
	if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
		return $query_args;
	}
	
	// process if specific view
	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
		if(isset($_GET['ids'])) {
			$query_args['post__in'] = explode(',', $_GET['ids']);
		}
	}
	return $query_args;
}

Note: Please replace 12345 with the actual view's ID.

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.

This function will convert the comma-separated IDs into an array and then use them in the query.

#1970797

Wonderful! Works perfectly. Thanks a lot, very much appreciated!