Title: get_pages in functions.php problem
Last modified: August 19, 2016

---

# get_pages in functions.php problem

 *  Resolved [slee](https://wordpress.org/support/users/slee/)
 * (@slee)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/get_pages-in-functionsphp-problem/)
 * I have the following code in my functions.php file:
 *     ```
       $triplistarray = array();
       $tripparent = 2;
       $args=array(
         'child_of' => $tripparent
       );
       //$gettrips = get_pages($args);
       if ($gettrips) {
         foreach($gettrips as $trip) {
       	setup_postdata($trip);
       	if ($post->post_parent != $parent ) {
       		 array_push($triplistarray,$post->ID);
       	}
         } // foreach($pages
       } // if $pages
       ```
   
 * the call to get_pages seems to be breaking the front end of the website is there
   anyway around this?

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/get_pages-in-functionsphp-problem/#post-1251828)
 * Just a guess, but shouldn’t that bit of code be surrounded by a function structure?
 *     ```
       function my_page_array($arg) {
   
       your code
   
       }
       ```
   
 * Not sure what $parent is…
 * Also not sure you need setup_postdata there, just use
    `$parentid=$trip->post_parent;`
 *  Thread Starter [slee](https://wordpress.org/support/users/slee/)
 * (@slee)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/get_pages-in-functionsphp-problem/#post-1251851)
 * thanks for that you were right i managed to figure it out before you posted it.
 * ive got another problem though i tried using your code but it didnt fix it.
    
   basically i cant get the data from the post so i cant get the parentid for example.
   here is my code:
 *     ```
       $triplistarray = array();
       $tripparent = 2;
       $args=array(
         'child_of' => $tripparent,
       );
       $gettrips = get_pages($args);
       if (@count($gettrips)) {
         foreach($gettrips as $post) {
       	$parentid=$trip->post_parent;
       	if ($post->post_parent !== $parentid ) {
       		 array_push($triplistarray,$post->ID);
       	}
         } // foreach($pages
       } // if ($pages
       ```
   
 * so at the moment the `if ($post->post_parent !== $parentid ) {` doesnt work
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/get_pages-in-functionsphp-problem/#post-1251876)
 * Start over….what’s the goal here? What is it you are trying to accomplish with
   that code?
 *  Thread Starter [slee](https://wordpress.org/support/users/slee/)
 * (@slee)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/get_pages-in-functionsphp-problem/#post-1251881)
 * essentially i am creating a custom write panel that has a dropdown in it with
   a list of the grandchildren pages that you helped me with before. the idea is
   that someone can submit a post in the report category and can then assign the
   report to a trip using the id into a custom field. the above code is being used
   to get the list of grandchildren from the top parent (the grandparent). however
   since this is now in the function file and inside a function it isn’t getting
   the post data such as the parent id and title.
 * here is all the code:
 *     ```
       $new_meta_boxes =
       array(
       "triplist" => array(
       "type" => "select",
       "std" => "",
       "name" => "asigntrip",
       "title" => "Assign Trip",
       "description" => "")
       ); 
   
       function new_meta_boxes() {
       	global $post, $new_meta_boxes;
   
       $triplistarray = array();
       $tripparent = 2;
       $args=array(
         'child_of' => $tripparent,
       );
       $gettrips = get_pages($args);
       if (@count($gettrips)) {
         foreach($gettrips as $post) {
       	$parentid=$trip->post_parent;
       	if ($trip->ID !== $parentid ) {
       		 array_push($triplistarray,$post->ID);
       	}
         } // foreach($pages
       } // if ($pages
   
       	foreach($new_meta_boxes as $meta_box) {
   
       		echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
   
       		echo'<h2>'.$meta_box['title'].'</h2>';
   
       		if( $meta_box['type'] == "text" ) { 
   
       			$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
   
       			if($meta_box_value == "")
       				$meta_box_value = $meta_box['std'];
   
       			echo'<input type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" size="55" />';
   
       		} elseif ( $meta_box['type'] == "select" ) {
   
       			echo'<select name="'.$meta_box['name'].'_value">';
       			echo'<option value="">select trip</option>';
       			foreach ($triplistarray as $option) {
   
       				if ( get_post_meta($post->ID, $meta_box['name'].'_value', true) == $option ) {
       					$sel =  ' selected="selected"';
       				} elseif ( $option == $meta_box['std'] ) {
       					$sel = ' selected="selected"';
       				}
       				echo'<option value="'.$option.'"'. $sel .'>'. $post->post_title .'</option>';
   
       			}
   
       			echo'</select>';
   
       		}
   
       		echo'<p><label for="'.$meta_box['name'].'_value">'.$meta_box['description'].'</label></p>';
       	}
       }
   
       function create_meta_box() {
       global $theme_name;
       if (function_exists('add_meta_box') ) {
       add_meta_box( 'new-meta-
       boxes', 'More Info', 'new_meta_boxes', 'post', 'normal', 'high' );
       }
       }
   
       function save_postdata( $post_id ) {
       global $post, $new_meta_boxes;
       foreach($new_meta_boxes as $meta_box) {  
   
       // Verify
       if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
       return $post_id;
       }  
   
       if ( 'page' == $_POST['post_type'] ) {
       if ( !current_user_can( 'edit_page', $post_id ))
       return $post_id;
       } else {
       if ( !current_user_can( 'edit_post', $post_id ))
       return $post_id;
       }  
   
       $data = $_POST[$meta_box['name'].'_value'];  
   
       if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
       add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
       elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
       update_post_meta($post_id, $meta_box['name'].'_value', $data);
       elseif($data == "")
       delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
       }
       }
   
       add_action('admin_menu', 'create_meta_box');
       add_action('save_post', 'save_postdata');
       ```
   
 * i really appreciate your help
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/get_pages-in-functionsphp-problem/#post-1251885)
 * Sorry, that’s beyond me…maybe someone else can jump in with an idea.
 *  Thread Starter [slee](https://wordpress.org/support/users/slee/)
 * (@slee)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/get_pages-in-functionsphp-problem/#post-1251889)
 * the only problem i am having is getting the data from the post in this:
 *     ```
       $triplistarray = array();
       $tripparent = 2;
       $args=array(
         'child_of' => $tripparent,
       );
       $gettrips = get_pages($args);
       if (@count($gettrips)) {
         foreach($gettrips as $post) {
       	$parentid=$trip->post_parent;
       	if ($trip->ID !== $parentid ) {
       		 array_push($triplistarray,$post->ID);
       	}
         } // foreach($pages
       } // if ($pages
       ```
   
 * i need to get the parent id as it goes through the loop and also the title to
   be used in the dropdown
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/get_pages-in-functionsphp-problem/#post-1251890)
 * this
 *     ```
       foreach($gettrips as $post) {
       ```
   
 * should be
 *     ```
       foreach($gettrips as $trip) {
       ```
   
 * and again this doesn’t seem right:
 *     ```
       if ($trip->ID !== $parentid )
       ```
   
 *  Thread Starter [slee](https://wordpress.org/support/users/slee/)
 * (@slee)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/get_pages-in-functionsphp-problem/#post-1251900)
 * thanks so much for your help i got it work 🙂
    using this:
 *     ```
       $triplistarray = array();
       $tripparent = 2;
       $args=array(
         'child_of' => $tripparent,
       );
       $gettrips = get_pages($args);
       if (@count($gettrips)) {
         foreach($gettrips as $trip) {
       	if ($trip->post_parent != $tripparent ) {
       		 array_push($triplistarray,$trip->ID);
       	}
         } // foreach($pages
       } // if ($pages
       ```
   
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/get_pages-in-functionsphp-problem/#post-1251904)
 * Whew. Good to hear you got it working. Congrats.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘get_pages in functions.php problem’ is closed to new replies.

## Tags

 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [get pages](https://wordpress.org/support/topic-tag/get-pages/)

 * 9 replies
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 7 months ago](https://wordpress.org/support/topic/get_pages-in-functionsphp-problem/#post-1251904)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
