Title: Page array not working
Last modified: August 20, 2016

---

# Page array not working

 *  [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/)
 * I’m using this query on a custom page
 *     ```
       <?php
       $args = array(
         'post_type' => 'page',
         'post__in' => array(get_post_meta($post->ID, "page_id", true))
         );
       query_posts($args);
       ?>
       ```
   
 * So I’m adding the page ids I want to call in a custom field called “page_id”
 * So for example in that field I have 220,221,222 but only the first page is getting
   called.
 * Any ideas?

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/#post-3101208)
 * Try just using `get_post_meta($post->ID, "page_id")`
 * > If only $id is set it will return all meta values in an associative array.
   > 
   > If $single is set to false, or left blank, the function returns an array containing
   > all values of the specified key. If $single is set to true, the function returns
   > the first value of the specified key (not in an array)
 * [http://codex.wordpress.org/Function_Reference/get_post_meta](http://codex.wordpress.org/Function_Reference/get_post_meta)
 *  Thread Starter [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/#post-3101249)
 * Umm, I tried this instead
 *     ```
       <?php
       $args = array(
         'post_type' => 'page',
         'post__in' => get_post_meta($post->ID, "page_id")
         );
       query_posts($args);
       ?>
       ```
   
 * But still only one page displays.
 * I then tried
 *     ```
       <?php
       $args = array(
         'post_type' => 'page',
         'post__in' => array(get_post_meta($post->ID, "page_id", false))
         );
       query_posts($args);
       ?>
       ```
   
 * and nothing displays.
 * I tried this, just to make sure it was working when not using the custom field
   value
 *     ```
       <?php
       $args = array(
         'post_type' => 'page',
         'post__in' => array(361,363)
         );
       query_posts($args);
       ?>
       ```
   
 * and it displays okay
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/#post-3101261)
 * Try:
 *     ```
       <?php
       $not_pages = get_post_meta($post->ID, "page_id");
       print_r($not_pages);
       $args = array(
         'post_type' => 'page',
         'post__in' => $not_pages;
         );
       query_posts($args);
       ?>
       ```
   
 *  Thread Starter [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/#post-3101266)
 * Getting a syntax error:
 *     ```
       Parse error: syntax error, unexpected ';', expecting ')' in /home/mysite/public_html/site/wp-content/themes/toolbox/template-collection.php on line 20
       ```
   
 *  Thread Starter [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/#post-3101267)
 * I used this instead
 *     ```
       <?php
       $not_pages = get_post_meta($post->ID, "page_id");
       print_r($not_pages);
       $args = array(
         'post_type' => 'page',
         'post__in' => $not_pages
         );
       query_posts($args);
       ?>
       ```
   
 * But I get the following on the page:
 * Array ( [0] => 363, 365 )
    Title of page
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/#post-3101272)
 * > But I get the following on the page:
 * this is because your custom field, and therefore the `$not_pages` variable, contains
   a single string of comma-separated numbers – not an array;
 * try to use the php function `explode()` to turn that into an array:
 * [http://php.net/manual/en/function.explode.php](http://php.net/manual/en/function.explode.php)
 *  Thread Starter [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/#post-3101273)
 * Blimey! Thanks for that but I have got absolutely zero idea where to start with
   exploding stuff.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/#post-3101288)
 * > I have got absolutely zero idea where to start with exploding stuff.
 * did you try to read and apply the linked tutorial?
 * based on you last posted code section:
 *     ```
       $not_pages = explode(',',get_post_meta($post->ID, "page_id"));
       ```
   
 * (untested)
 *  Thread Starter [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/#post-3101299)
 * Thanks again. I just realised that I asked a similar question on here over 2 
   years ago but I can’t for the life of me get this to work in a query posts function
 *     ```
       <ul>
        <?php
       $key = get_post_meta($post->ID, 'page_id', true);
       $key = explode(',', $key);
       function trim_value(&$value)
       {
           $value = (int)trim($value);
       }
       array_walk($key, 'trim_value');
       $myposts = get_posts(array('post__in' => $key, 'posts_per_page' => -1));
       foreach($myposts as $post) :
          setup_postdata($post);
        ?>
           <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endforeach; ?>
        </ul>
       ```
   
 * I’d like the above code to work in this format:
 *     ```
       <?php
       // My query here
       ?>
       <?php while (have_posts()) : the_post(); ?>
       <p><?php the_title() ?></p>
       <?php endwhile;?>
       ```
   
 *  Thread Starter [greencode](https://wordpress.org/support/users/greencode/)
 * (@greencode)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/#post-3101343)
 * I managed to get this to work…
 *     ```
       <?php
       $key = get_post_meta($post->ID, 'page_id', true);
       $key = explode(',', $key);
       function trim_value($value)
       {
           $value = (int)trim($value);
       }
       array_walk($key, 'trim_value');
        $myposts = get_posts(array('post_type' => 'page', 'post__in' => $key, 'showposts' => -1, 'orderby'=> date, 'order'=> DESC));
        foreach($myposts as $post) :
          setup_postdata($post);
        ?>
       <h2><?php the_title() ?></h2>
        <?php endforeach; ?>
       ```
   

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

The topic ‘Page array not working’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 3 participants
 * Last reply from: [greencode](https://wordpress.org/support/users/greencode/)
 * Last activity: [13 years, 7 months ago](https://wordpress.org/support/topic/page-array-not-working/#post-3101343)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
