Does hooking into the same action multiple times drain memory?

I guess you mean

add_action( 'pre_get_posts', 'private_groups' );
add_action( 'pre_get_posts', 'search_results' );
add_action( 'pre_get_posts', 'name_profiles' );

versus

add_action( 'pre_get_posts', 'combined_into_single_callback' );

You can just check the difference using e.g. memory_get_usage() and timer_stop(). Many good plugins out there to help with that.

I would say go with the first one, as they seems to be unrelated callbacks and probably easier to maintain.

I would expect that to be mostly dependent on the callback functions you run there: private_groups(), search_results() and name_profiles() versus combined_into_single_callback().

The hook setup itself shouldn’t be resource draining, as it’s just three new extra elements in a global array.

What you should be careful about, when using the pre_get_posts, is to target only the corresponding WP_Query instance and avoid running expensive code blocks for all the instances.

Leave a Comment