0

I want to display all authors by their recent published post date. With the code below, I can easily list the authors. But I want to display each author one time. I made a research and found DISTINCT statement but couldnt make it. Could you please help me about this?

Thank you

    global $wpdb;
    $querystr = "SELECT * from wp_users, wp_posts WHERE wp_users.ID = wp_posts.post_author AND wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish' ORDER BY wp_posts.post_date DESC LIMIT 5";
      $news=$wpdb->get_results($querystr, OBJECT);
    foreach($news as $np)
          {
            $author_displayName = $np->display_name;
            $post_date= $np->post_date;
}
fatihsolhan
  • 565
  • 5
  • 19
  • Write DISTINCT before your *. Might not give desired output depending on what the * is returning from the two tables. – dfundako Nov 08 '17 at 21:22

1 Answers1

1

EDIT for working solution, original below the line: Just found this older solution on SO: MySQL order by before group by

They want to extract the full posts table, but apparently you're only interested in the username and last post date, so this should be a better solution without nesting two queries:

SELECT max(wp_posts.post_date) MaxPostDate, wp_users.display_name
FROM wp_posts
JOIN wp_users ON (wp_posts.post_author = wp_users.id)
WHERE wp_posts.post_status='publish' AND wp_posts.post_type='post'
GROUP BY post_author

ATTN: In MySQL >= 5.7.5 and when ONLY_FULL_GROUP_BY SQL mode is enabled you'll have to surround all columns that are not mentioned in the GROUP BY statement with the ANY_VALUE() function. i.e.:

SELECT max(wp_posts.post_date) MaxPostDate, ANY_VALUE(wp_users.display_name)

SELECT wp_users.display_name, wp_posts.post_date
FROM wp_users
LEFT JOIN wp_posts ON (wp_users.ID = wp_posts.post_author)
WHERE wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish' 
GROUP BY wp_users.display_name
ORDER BY wp_posts.post_date DESC
LIMIT 5

This should give you the desired output.

Now to the explanation:

  1. SELECT and then list only those fields you really need, as that may make the query faster.
  2. only use one from and then JOIN the other table. In this case this is only a matter of preferred syntax and clarity
  3. The where clauses are reduced by the one for matching the two tables together
  4. using GROUP BY you can select different columns to show from the ones you want to be unique
  5. ORDER BY and LIMIT stay just the way they are

The downside of this: GROUPing is done BEFORE ordering. Thus you'll get the results in whatever order their primary keys occur and the ordering by time will only apply to the order in which the results will be displayed, not which times will be shown for each user.

Mastacheata
  • 1,866
  • 2
  • 21
  • 32
  • Thank you for answer, but it still gives same author more than one time. – fatihsolhan Nov 09 '17 at 01:23
  • By the way, there was a typo on your code. LEFT JOIN wp_post ON should be LEFT JOIN wp_posts ON @Mastacheata – fatihsolhan Nov 09 '17 at 01:26
  • Fixed the typo.Sorry, my fault. DISTINCT only limits the results to distinct tuples of all fields. (i.e.: if either user or time differs, it's a different entry) You'll have to divert to GROUP BY if you want to select more fields than you want to have distinct ones. I'll edit the answer right away. – Mastacheata Nov 09 '17 at 02:07
  • Thank you again for your help, I tried the new code on phpmyadmin and I got error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY wp_users.display_name LIMIT 5' at line 6 – fatihsolhan Nov 09 '17 at 02:13
  • Oops sorry, switch the GROUP BY and ORDER BY lines and the result should at least show only one row per user (the times will most likely be off, though. Group BY will FIRST group/limit the results and only then apply an ordering, so you'll have to solve this with a subselect) – Mastacheata Nov 09 '17 at 02:15
  • I switched the group by and order by. But I got error again :) #1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'demos.blog.wp_posts.post_date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by My sql knowledge is not good, thats why I am asking for every error. Sorry about that. – fatihsolhan Nov 09 '17 at 02:20
  • No idea why you get that error. The statement itself wokrs just fine on my MariaDB 10.2 server. I've updated my answer to provide a better solution to your original problem though and a link with a somewhat broader solution from an older SO post. – Mastacheata Nov 09 '17 at 02:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158559/discussion-between-mastacheata-and-solhan). – Mastacheata Nov 09 '17 at 02:37