How does WordPress call functions attached to a certain action hook before calling functions attached to other hooks

WordPress doesn’t execute all the actions and filters (i.e. hooks) at the same time using some sort of priority, instead, execution order of these action and filter hooks are hard coded within WordPress core PHP files.

Within WordPress source CODE, search for the following four function calls (using any text editor’s find in files feature):

  1. apply_filters => applies a specific filter.

  2. apply_filters_ref_array => applies a specific filter, specifying arguments in an array.

  3. do_action => does a specific action.

  4. do_action_ref_array => does a specific action, specifying arguments in an array.

You’ll see that each of the above function calls are used multiple times within the WordPress source CODE and execution order (priority) of the corresponding action and filter hook depends on when WordPress makes those function calls.

As PHP itself executes CODE line by line, execution order of those functions and their corresponding hooks also depend on which PHP file & in what line those function calls are.

Note: Although execution order of each specific action & filter hook is pre-determined within the CODE, execution order for multiple use of a single hook is determined by the $priority parameter.

WordPress hard codes these function calls to make sure the execution order of these action & filter hooks are predetermined. Otherwise developers would have real hard time coding themes & plugins.

Note: Since 4.7 WordPress implements all of the above four functions internally using WP_Hook class. So you may also take a look at that class to understand how all these are done internally.