7

Currently I have three functions that run on pre_get_posts. I call them like this:

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

This allows me to keep the functions neat and separate, but is calling to the same action three times, versus calling a combined function once, a drain on resources?

  • 1
    There is no reason that hooking three times in the same action hook to do three things separately would consume appreciably more memory than doing the three things into one hook. Organize the code in the best way for you and your partners, not only hooking in the same hook multiple times, even separating that hooks into different plugins can be very good approach to make the job easier. – cybmeta Jan 15 '16 at 11:35
5

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.

  • Yes, I meant what I have now vs a combined function as you suggest. – SinisterBeard Jan 15 '16 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.