1

I am modifying my main queries with a function, modify_main_query($query), that determines which page is being accessed by the user via if/else statement, and then modifies the query accordingly. I am adding that function using this hook:

The main function's hook

add_action('pre_get_posts', 'modify_main_query')

One of my if clauses (for a certain page) inside of modify_main_query($query) calls a small function, called get_exclusion_IDs_for_cats_by_name($cat_names).

this is a simplified version:

The Main hook-added function

function modify_main_query($query){

if ( is_front_page() ) {

   [...do stuff]

}

elseif ( is_archive() ) {

    $cat_names=array('apple', 'orange'); 
    get_exclusion_IDs_for_cats_by_name($cat_names); // small function I want to add 
}

else {NULL;}

endif;

My question

What is the proper way to add this small function, get_exclusion_IDs_for_cats_by_name($cat_names), that is called by my action-hooked function, modify_main_query($query), to functions.php? Do I just throw the small function into functions.php, or do I need a separate hook for the mini-function? It works if I just add the called function separately without a hook but I wanted to check, to make sure I am doing it in the preferred way.

Thanks!

3

This looks good. If we assume get_exclusion_IDs_for_cats_by_name($cat_names); works correct then you are doing it right.

Your question may be rephrased like this:

Can I call other functions inside the custom actions?

And the answer is yes. The only thing you need to be careful is that your other function is available from where you call it.

  • So no other hooks are needed? It is considered acceptable practice just to declare the little function that is called by the main hooked function inside of functions.php, without any extra work? – CoderScissorhands Jan 30 '17 at 19:54
0

If you are not using your own-built theme, then it is usually recommended to create a Child Theme, then put your unique functions in that Child Theme's functions.php file.

That way, your functions will not be overwritten with an update to the theme you are using. It's always a good idea to never make changes to core files, nor to themes you are using.

If you are building your own theme, then add functions where needed.

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.