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

Read More
<?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?

Related posts

Leave a Reply

2 comments

  1. 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