All you need is to use pre_get_posts
action to hook to add author_id
to the query based on your custom custom_user_id
query var:
add_action( 'pre_get_posts', 'filter_by_custom_user_id' );
function filter_by_custom_user_id( $query ) {
// Apply only on frontend, for gallery post type archive, for main query and if custom_user_id query var is set.
if( ! is_admin()
&& $query->is_main_query()
&& is_post_type_archive( 'gallery' )
&& isset( $query->query_vars['custom_user_id'] ) {
$query->set( 'author_id', (int) $query->query_vars['custom_user_id'] );
}
}
IMPORTANT: I see that you are flushing rewrite rules on every page load using init
action hook. Flusing rewrite rules should be done only on plugin activation and deactivation. Other than that hooks, flushing rewrite rules should be done only specifically when you know that rewrite rules have changed and they need to be rebuilt. Note that flushing rewrite rules perform dabase operations that are not needed to do only one time, not on every page load.