use query_posts to return a post OR a page by ID

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?

2 Answers
2

Try this:

query_posts(array(
    'p' => $topright,
    'post_type' => array('post', 'page'),
));

Leave a Comment