I have a view shortcode that displays particular posts based on the shortcode parameter "postid." Unless I'm missing something, there doesn't seem to be any simple way to order the results by the original order of the arguments. For instance, I want the posts in the following example to display in the same order they are entered:
[wpv-view name="directory-card" postid="4605,4797,4655"]
However, I don't see any equivalent option to "original order of arguments." Is there a simple alteration I can make to the $query_args?
Hi Corey,
Thank you for contacting us and I'll be happy to assist.
You can use filter "wpv_filter_query" to set "$query_args['orderby']" to "post__in", which will show posts in the order they're added in the argument.
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )
For example, you can add the following code at the bottom of your active theme's "functions.php" file and update the "xyz" with your view's actual ID:
// custom function to filter output of view based on shortcode parameter
add_filter( 'wpv_filter_query', 'filter_posts_order_fn', 1000 , 3 );
function filter_posts_order_fn( $query_args, $view_settings ) {
if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == xyz) ) {
$query_args['orderby'] = 'post__in';
}
return $query_args;
}
I hope this helps! Please let us know if you need any further assistance.
Thanks, Waqar! That works perfectly. I could have sworn I tried to set "post__in" as the "orderby" arg. I must have had something wrong. Thanks again.