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

in my home page I'd like to display some pages and posts title using the custom fields.

Unfortunately my code prints only posts title.

If there is a key "highlights" print the title.

Here my code:

<?php while (have_posts()) : the_post(); ?>

    <?php if ( get_post_meta($post->ID, 'highlights', true)) { ?>

        <?php the_title() . "<br>"; ?>

    <?php } ?>

<?php endwhile; ?>

How can I print both? Posts and pages title?

Thank you

share|improve this question
Not sure exactly what you're asking, are you using a static page for your home page? In that case I think you should take a look at get_posts (codex.wordpress.org/Template_Tags/get_posts). With get_posts you can list posts from a certain category or post_type on your home page. – Richard B Nov 14 '12 at 12:00
I'd like to print the title of the pages and the posts. In my code, I print only the posts title, not pages. – Ale Nov 14 '12 at 12:45
Where do you want to print the page titles? Alongside the post titles or in a separate area like a sidebar? – Richard B Nov 14 '12 at 12:52
Are you referring to your blog posts index page, or your site front page specifically? Are you using a custom query, or the main query? – Chip Bennett Nov 14 '12 at 12:55
I'd like to print the titles in the home page but not in a sidebar. Is it a problem? Do I have use a sidebar? – Ale Nov 14 '12 at 12:56

You're not being very specific so it's hard to give you any help. But if you would like to output both pages and posts in the same loop you need to customize your query so that it includes both posts and pages. Learn about http://codex.wordpress.org/Class_Reference/WP_Query.

Or if you want the posts to be outputted at one place and the pages in another place, http://codex.wordpress.org/Function_Reference/get_pages or maybe http://codex.wordpress.org/Function_Reference/wp_list_pages.


To mix posts and pages in one loop you need to query both post types. Try adding query_posts like this:

<?php query_posts('post_type=post,page'); ?>
<?php while (have_posts()) : the_post(); ?>

    <?php if ( get_post_meta($post->ID, 'highlights', true)) { ?>

        <?php the_title() . "<br>"; ?>

    <?php } ?>

<?php endwhile; ?>
share|improve this answer
Yes, I'd like to output both pages and posts title in the same loop in my custom home page (index.php). With the code I write, my loop only outputs posts title. Sorry for my bad english. – Ale Nov 14 '12 at 13:32
I've updated my answer, try query_posts. – Richard B Nov 14 '12 at 14:59
Your code doesn't works but the idea is good, so i changed my code. Here is it: $args = array( 'post_type' => array('post','page'), 'posts_per_page' => 5); – Ale Nov 14 '12 at 15:52
Sweet. Please mark this as solved if you're happy with the answer. – Richard B Nov 16 '12 at 9:34

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.