Difference between add_filter versus add_action
pre_get_posts
is an action and not a filter. $query
is passed by reference which is why CODE A works without returning anything.
CODE B returns $query
but again that code works because query has been passed by reference. The return value of the hook isn't assigned to anything.
do_action_ref_array( 'pre_get_posts', array( &$this ) );
add_action
and add_filter
are used in different contexts but the code is the same (add_action
is an alias of add_filter
). While both sets of code posted will work the correct usage is add_action
.