0

This function will show the last 3 posts from any category on front page:

  // Only top 3 posts from CHR category
  add_action('pre_get_posts', 'ad_filter_categories');
  function ad_filter_categories($query) {
  if ($query->is_main_query() && is_home()) {
  $query->set('category_name','chatham-house-rules');
  $query->set('showposts', 3);
}

I want to do this twice more with two other categories but it will only ever work with one. It also defaults to the posts_per_page setting in the Admin panel. Any ideas how I can achieve 3 latest posts from 3 categories in specific order on front page?

  • Yes - a page template for your home page will allow you to do this. You'll need multiple loops or another way to create your three separate category sections. codex.wordpress.org/Class_Reference/… – jdm2112 May 5 '15 at 15:49
0

There are multiple ways to go about this, but one way I would consider if I were you would be to do it like this. On the index.php or whatever page the posts appear on do the following three times each with different categories:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$arguments = array(
    'category_name' => 'category'
    'posts_per_page' => 3,
    'paged' => $paged
);
$wp_query->query($arguments);
if (have_posts()) :
   while (have_posts()) : the_post();
       the_content();
    endwhile;
endif;

The category_name can be a comma delimited string that should allow you to have three or more categories.

The paged parameter is needed when setting the posts per page this way. If that isn't done, then the posts don't show up correctly on the subsequent post pages.

The WP_Query reference, shows all the filters that can used in the query. It also offers up an alternative way of using the query.

  • Thanks, tried that but it now only shows three posts (ignoring the featured top post and the image slider is missing). I have added another cat but it shows three in total. I need 3 from each category in category order - not by date posted. – buksida May 6 '15 at 7:33
  • I somewhat misunderstood what you were asking. I have changed my answer a bit, but basically all you need to do is run that query and the WordPress loop three times, each with the category you want to show. As far as the image slider, I can't speak on that. I don't know how you created that. If you want to show the featured top post in addition to the three category sections, you will have to run another query looking for that as well. I assume the top post is a sticky post, if that is the case use the sticky post query at codex.wordpress.org/Class_Reference/WP_Query – Jon Black May 6 '15 at 16:00
0

WP_Query allows you to define a whole lot of complex selection criteria to match individual posts to. However, what you want is effectively a criterium for the complete set that is returned from the query. This leaves you with two possible approaches:

  1. Use one query that returns a sufficient amount of posts, so you are sure at least one post from each category is present. Then loop through that set an pick the first post of each category.
  2. Use three queries that return one post from each category and merge the results.

Which one is best depends on the circumstances. The latter one is logically most sound, but queries the database thrice, so may be a drain on your resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.