0

I want to show only one latest post by each author and found this code that it working. However, it takes too long to load the page since I have over 100K authors and 100K posts in database. How can I optimize this code?

<?php
  //Displaying latest post per author on front page
  function filter_where($where = '') {
    global $wpdb;

    $where .= " AND wp_posts.id = (select id from {$wpdb->prefix}posts p2 where p2.post_status = 'publish' and p2.post_author = {$wpdb->prefix}posts.post_author order by p2.post_date desc limit 0,1)";
    return $where;
  }
  add_filter('posts_where', 'filter_where');
  query_posts($query_string);

//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();

...
  ?>

Here is an original article for the code.

http://www.dbuggr.com/smallwei/show-latest-post-author-wordpress-front-page/

0

Edit: I just tested and it works perfectly. You'll also want to remove pagination from the author template.

I haven't tested it, but have you tried pre_get_posts?

function author_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
  if ($query->is_author()) {
    $query->set( 'posts_per_page', 1 );
   }
 }
}
add_action('pre_get_posts','author_filter');
4
  • I just tested it but it does not filter out posts from an author. It suppose to show only one latest post by author. Here is my code grab.by/HOK8 – Ohsik Jun 4 '15 at 16:38
  • This is actually a category list page. that will show list of a latest post by all the users who have posts. That is why I need pagination on the page. – Ohsik Jun 4 '15 at 16:46
  • Sorry. I thought you were in the author template. You may have already seen it, but have you tried any of the solutions here? wordpress.stackexchange.com/questions/78117/… – Josh M Jun 4 '15 at 16:56
  • Thank you for the link. I just tried that but no luck on my situation. – Ohsik Jun 4 '15 at 17:12
0

Wow! A subquery in the WHERE clause! That is a nasty one. :)

I can't test this right now (on the phone, this all memory) but let me see what I can do. I think that all you need is a GROUP_BY filter:

function group_by_author($groupby) {
  global $wpdb;
  return " {$wpdb->posts}.post_author";
}
add_filter('posts_groupby','group_by_author');

Then create a new Wp_Query with other relevant arguments -- not query_posts! Not ever!

Proof of concept:

function group_by_author($groupby) {
  global $wpdb;
  return " {$wpdb->posts}.post_author";
}
add_filter('posts_groupby','group_by_author');

$args = array(
  'post_type' => 'post',
  'posts_per_page' => -1,
  'ignore_stickie_posts' => true,
  'order' => 'ASC',
);
$q = new WP_Query($args);
var_dump($q);
if ($q->have_posts()) {
  while ($q->have_posts()) {
    $q->the_post();
    the_title();
    the_author();
    echo '<br>';
  }
}
7
  • I tried your code but nothing shows up on the page. Here is my code. what am I missing? grab.by/HOL0 – Ohsik Jun 4 '15 at 16:42
  • I'll have to test later – s_ha_dum Jun 4 '15 at 17:47
  • after looking through your previous answer (wordpress.stackexchange.com/questions/78117/…), removing GROUP BY seems to be working but ended up at the same issue that it shows the oldest post instead of latest. Also pagination will get messed up cause it still does calculation on total number of posts. – Ohsik Jun 4 '15 at 22:04
  • @Ohsik : see the edit. – s_ha_dum Jun 5 '15 at 20:33
  • Thank you for the update. I just tried that code and this is what I get grab.by/HR2K. does not seem to be working for me. – Ohsik Jun 5 '15 at 21:09

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.