Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Probably pretty simple and more PHP-related than pure WP, but how would one pass the $page_id generated in the first code snip into the new WP_Query () in the second code snip?

What I need to do - in a page template - is dynamically get the current page ID and pass that to the new WP_Query in order to display content for that current page. This is for a page template that will display different content according to the page_id, somewhat opposite of what a page template usually does with the loop.

I don't need pagination or anything else; the new queries will display small amounts of content in a jQuery UI tab structure.

Global wp_query to get current page ID (and echo it for testing purposes):

<?php global
$wp_query;
$page_id = $wp_query->get_queried_object_id();
echo $page_id; ?>

Standard new WP_Query for the content:

<?php $my_query = new WP_Query( **need $page_ID here** ); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
share|improve this question
    
You've got a page id, from the first snippet, but what do you want to compare it to in your wp_query constructor? –  anu Jul 10 '12 at 18:01
    
I want the page_id to be the WP_Query value, as noted above in the second code snip. –  songdogtech Jul 10 '12 at 19:06
    
Maybe I'm misunderstanding you. If $page_id is 4, is this what you want to do: $my_query = new WP_Query(4)?? –  anu Jul 10 '12 at 21:00
    
Yes, that's what I want to do. I want to pass the page_id to the query so the template displays content according to the page_id. –  songdogtech Jul 10 '12 at 21:27
    
ok, but you can't just put a value as the parameter - you need to at the very least do this: new WP_Query( 'page_id=4' ); –  anu Jul 10 '12 at 22:07
show 1 more comment

2 Answers

up vote 1 down vote accepted
+50

If you're on the same page and you just need to display the same page's content again, you can just call the_content multiple times.

To answer your question, however:

You'll use the page_id argument for WP_Query (you could also probably use p). You can even remove your global $wp_query and the like.

<?php
$new_query = new WP_Query(array(
   'page_id' => get_queried_object_id()
));
share|improve this answer
    
Looks like that should work, but your new query with get_queried_object_id just gives me white space, so there's an error somewhere in it. What I want to do is use a page template that will dynamically display different content according to the page_id, opposite of what a page template usually does with the loop. –  songdogtech Jul 8 '12 at 18:35
    
My mistake; this works fine. Even with removing the global $wp_query. Thanks! –  songdogtech Jul 17 '12 at 16:58
add comment

You don't need first snippet. Just use get_the_ID() on second snippet.

<?php $my_query = new WP_Query( ** use get_the_ID() here ** ); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
share|improve this answer
1  
not sure why you need query there though. Page content is already available for you. –  Sisir Jul 8 '12 at 18:05
    
This is for a page template that will dynamically display different content according to the page_id. –  songdogtech Jul 8 '12 at 18:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.