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.
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
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.
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.
Wonderful! Works perfectly. Thanks a lot, very much appreciated!