use static for the good page ids
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 20 Dec 2017 18:53:27 +0000 (13:53 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 20 Dec 2017 18:53:27 +0000 (13:53 -0500)
to prevent hitting database twice.

functions.php

index 30c6523..101ebbf 100644 (file)
@@ -98,17 +98,19 @@ add_filter('pre_get_posts','SearchFilter');
 
 function glm_get_visable_page_ids()
 {
-    $publishPageIds = array();
-    // Find all pages marked published
-    $publishedPages = get_pages(
-        array(
-            'post_type'     => 'page',
-            'post_status'   => 'publish',
-        )
-    );
-    foreach ( $publishedPages as $page ) {
-        if ( !$page->post_password ) {
-            $publishPageIds[] = $page->ID;
+    static $publishPageIds = array();
+    if ( empty( $publishPageIds ) ) {
+        // Find all pages marked published
+        $publishedPages = get_pages(
+            array(
+                'post_type'     => 'page',
+                'post_status'   => 'publish',
+            )
+        );
+        foreach ( $publishedPages as $page ) {
+            if ( !$page->post_password ) {
+                $publishPageIds[] = $page->ID;
+            }
         }
     }
     return $publishPageIds;