I'm making a mapping between page-id's and category-id's, that's being used several places.

However I read from this post, that one should never use global variables if one could avoid it. But I would argue that this would be smart to have as a global variable, so it's not a function that should be executed several time (and iterate over several ID's every time).

But I'm unsure how to do this. Here's what I got:

 * Should this be here, in order to make it global?
$page_category_mapping = array();
global $page_category_mapping;

 * Building the mapping... 
 * Should this tage the $page_category_mapping
 * as input? 
function build_page_category_mapping(){
  // Going over all pages and maps them to a category
add_action( 'init', 'build_page_category_mapping' );

 * I assume this function should take the array as input, right?
function a_funtion_that_uses_the_mapping( $page_category_mapping ){
  echo '<pre';
  print_r( $page_category_mapping );
  echo '</pre';
add_shortcode( 'list_mapping', 'a_funtion_that_uses_the_mapping' );
  • What exactly is the purpose of this mapping? What problem does it solve? Could you not just call wp_get_object_terms to fetch the categories? WP already caches these things in memory, so if you're trying to improve performance with a global variable, you'll see no gains – Tom J Nowell Apr 18 '18 at 15:04
  • I've built a function, that automatically creates a category for every page there is. In every level, then the category-name is unique. But for two children, that has different parents, then the name can be identical. So when I'm creating a post, then I can't just go through the categories and find the first categor-name that matches the name of the page. It needs to be the right one. And instead of doing that calculation every time something like this needs to occur, then a global array with the mappings would be preferred (in my opinion). – Zeth Apr 18 '18 at 16:07
  • 1
    You'd still have to fill that array, which is going to be fetching a large amount of data, and will be costly. This sounds like an overcomplicated solution to a problem you haven't mentioned that didn't work out the way you expected. What's the original problem you were trying to solve or implement that required you to automatically create categories for every page? Are all these pages page templates with archives? Would it not be easier to just use a custom taxonomy and the archives and templates WP already provides? e.g. category.php? – Tom J Nowell Apr 18 '18 at 23:18

I figured it out myself.

function generate_the_excluded_array() {

  global $excluded_pages;
  $excluded_pages = array('Foo', 'Bar');
add_action( 'after_setup_theme', 'generate_the_excluded_array' );

And then it can be 'imported' using the global, as such:

function example_shortcode_definition() {

  global $excluded_pages;
  echo $excluded[0];
add_shortcode( 'example_shortcode', 'example_shortcode_definition' );

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.