Tutorial :How can I loop through posts as well as child pages to display them all by date in Wordpress 2.9



Question:

Some background info --

In wordpress I have my portfolio as a parent page with each item of work a child page. I also have a blog.

I want to display the most recent 6 items on the homepage whether they are from my portfolio or my blog.


I have a loop that displays the posts and on another page I have a loop that displays the child pages of a specific parent page. I only need to display the_title and the_post_thumbnail.

Is it possible to loop through both the posts and the child pages and display them in order of date (recent first).

So the final display would be a mixture of posts and child pages.

Could it be done by having a separate loop for pages and one for posts, then somehow adding the results to an array and then pull them out in date order (recent first).

Any help would be greatful.

Thanks


Solution:1

Use a custom query.

Example

$latest_content = new WP_Query(      array(          'posts_per_page'    => 6,          'post_type'   => 'any',          'post_status' => 'publish'      )  );  if ( $latest_content->have_posts() )  {      while ( $latest_content->have_posts() )      {          $post = $latest_content->next_post();          // Do anything you know about the loop.          print $post->guid . '<br>';      }  }  rewind_posts();  


Solution:2

I have not tried it myself, but I think you can use the get_posts template tag with post_type => 'any'.

Like so (default ordering is date descending):

<?php  $args = array('post_type' => 'any', 'numberposts' => 6);  $posts_and_pages = get_posts($args);  ?>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »