How to get contextual filter in hook_views_query_alter or hook_views_pre_execute?
The contextual filters are in $view->args.

$view->args is an array of arguments (contextual filters).

So you can do things like this:

/**
 * Implements hook_views_pre_execute().
 */
function MODULENAME_views_pre_execute(&amp;$view) {
  // Set the first contextual filter value.
  $view-&gt;args[0] = 1;
}