Querying both pages and posts

I am building an “advent calendar” for a client and they are wanting a combination of specific pages and custom posts appearing on the front page, in a specific order.

Things that need to be retrieved are Title, Excerpt (or Content up to ), Permalink, Featured Image and Custom Fields…

Read More

What’s the best way to go about retrieving these from the database? A custom SQL query? Some sort of Walker? A complicated Menu?

Related posts

Leave a Reply

2 comments

  1. Use get_posts() or filter 'pre_get_posts' on the front page.

    Sample code for get_posts():

    $my_post_array = get_posts( 
        array ( 
            'post_type' => array ( 'post', 'custom_type' ) 
        ) 
    );
    
    foreach ( $my_post_array as $post )
    {
        // do something awesome …
    }
    

    There are also parameters for a custom order by meta fields and so on. A filter on 'pre_get_posts' can do the same and more. It is just not as easy to read.

  2. I ended up specifying the posts, pages and custom posts in a custom Navigation menu, and created a Custom Walker to output the right information.