WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm building a custom homepage for a client where they want to have a few changeable boxes to link to specific pages, or posts, within the site. I've added custom fields to the homepage so that they need only enter the page/post ID, and then it will display the proper post or page. And I'd like it to be flexible enough that they can do a post or page.

Right now, my code for the box is

<?php query_posts('p='.$topright); ?>
<?php while (have_posts()) : the_post(); ?>
     {title and featured image}
<?php endwhile; ?>

where $topright is a variable already defined. (I've tested the variable with an echo and it is returning the proper ID number.)

Unfortunately, WordPress seems to require that I use p=ID if it is a post, and page_id=ID if it is a page. So, if I designate the ID for a post, it is working fine, but not if I desingate the ID for a page. Is there an alternative syntax I could use? Or, it there a conditional of some kind that might look a the ID and recognize if it is a post or a page so I could run the query with an IF ELSE?

share|improve this question
up vote 5 down vote accepted

Try this:

    'p' => $topright,
    'post_type' => array('post', 'page'),
share|improve this answer
Perfect. Fixed this exact problem I was having. – Ian Jun 17 '12 at 5:55

you can use 'post__in' in your query and if you are creating a custom query you should use WP_Query or get_posts and not query_posts , so something like this:

$my_query = new WP_Query();
$my_query->query(array( 'post__in' => array($topright)));
while ($my_query->have_posts()) : $my_query->the_post(); ?>
     {title and featured image}
<?php endwhile; ?>

Hope this helps

share|improve this answer

Your Answer


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.