Skip Navigation

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

This support ticket is created 3 years, 1 month ago. 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.

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 4 replies, has 2 voices.

Last updated by hispavista-s.l.E 3 years ago.

Assisted by: Waqar.

Author
Posts
#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

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.