0

I am trying to make a WP_Query that gets all pages / posts from the navigation id. I have the id's. I have a variable that holds the numbers and when I echo it out, this is exactly what I get

7,43,20,22,16,1051,18,26,9,24,28,10523

Reading in the codex, I should be able to use page_id. When I refresh the page, I do not get any posts or pages. Having tried many different versions of this query, this is where I am at right now.

 $page_query = new WP_Query( array('post_type' => array( 'post', 'page'), 'page_id' => array($number)));
1

If you have posts and pages then page_id isn't the correct parameter. You should probably use post__in. See WP_Query Parameters.

Additionally, post__in accepts an array, whereas you have a string, so you'll need to use PHP to explode the string into an array.

This is untested, but I think it should do it:

$ids = '7,43,20,22,16,1051,18,26,9,24,28,10523';
$ids = explode(",", $ids);
$page_query = new WP_Query( 'post__in' => $ids, 'nopaging' => true );

nopaging should do it, or "posts_per_page" => -1 if it doesn't.

| improve this answer | |
  • $number was already an array. I was using the implode function to add the comma because I thought the syntax for post__in would have been 1,2,3,4. As it is, there are no comas in the array. So wouldn't the syntax be wrong? The code you posted works better but only returns 3 out of 12 pages – Jamie Feb 2 '14 at 2:29
  • Look again at the Parameters link I posted. post__in definitely accepts an array. There shouldn't be commas in the array, each ID is a different value in the array. Only 3 are displayed? Under reading options, is "posts per page" set to 3? Possibly, just need to add a parameter to show all the posts. See edit. – helgatheviking Feb 2 '14 at 10:53
  • yep, I did have it settings set to 3. I can't believe I didn't think of that. Thank you for your help. – Jamie Feb 2 '14 at 17:05

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.