0

I am trying to investigate the correlation between the total number of posts published on a blog and the traffic it receive. I think it's not a huge task but my experience with coding with Wordpress is almost null.

I guess it should be something like a foreach which loops all the posts and list the publishing date for each of them. Then sorts them by publishing date and then it counts the number of published post.

Is my question clear enough?

3 Answers 3

2

the fastest way imho would be to create a custom sql statement:

global $wpdb;
$rows = $wpdb->query('SELECT DATE(post_date) AS date, COUNT(*) AS count FROM ' . $wpdb->posts . ' GROUP BY DATE(post_date) ORDER BY DATE(post_date)');
foreach ($rows as $row) {
    echo $row->date . ': ' . $row->count . '<br>';
}
4
  • Good answer, just a note, it is not recommended to use custom SQL as you loose the ability of important hooks and filters. Also, what if the tables in db change :-) Commented Apr 28, 2015 at 13:40
  • Can you also write the clear SQL it can be the easiest solution for me in this case.. And how do I get only the published post?
    – Revious
    Commented Apr 28, 2015 at 13:40
  • 1
    @Pieter: You're right - plain SQL is not future oriented and ignores hooks and filters. Additionally, the current SQL doesn't account for unpublished, private or versions of posts. It's just a fast way. On the other side: the post table definition hasn't changed for years - i'm pretty confident that such a sql would work for a long time ...
    – jjarolim
    Commented Apr 29, 2015 at 15:08
  • @jjarolim there is a problem.. it takes as posts every single revision.. I think I've found the correct query. SELECT DATE(post_date) AS date, concat(post_title) titles, COUNT(*) AS count FROM wp_posts WHERE post_status = 'publish' AND post_parent = 0 AND post_type = 'post' GROUP BY DATE(post_date) ORDER BY DATE(post_date)
    – Revious
    Commented May 8, 2015 at 10:05
2

You'll want to use WP_Query, along with the data parameters

e.g. years:

$years = array( 2015,2014,2013,2012 );
echo '<table>';
foreach ( $years as $year ) {
    $q = new WP_Query( array(
        'year' => $year,
        'fields' => 'ids'
    ) );
    echo '<tr><td>'.$year.'</td><td>'. $q->found_posts.'</td></tr>';
}
echo '</table>';

Note I specified only to grab the post IDs, this is to reduce the cost of the query. Doing it this way also means you can take better advantage of caching plugins

4
  • You would want to use 'fields' => 'ids' in there as well, this can get quite expensive to run such a query :-) Commented Apr 28, 2015 at 13:05
  • This solution doesn't scale well - the direct-sql-solution above starts one sql query and returns the count for every day since the blog started. Your solution would have to start one query per day which gets very inefficient very fast.
    – jjarolim
    Commented Apr 29, 2015 at 15:11
  • My solution can be cached using the standard APIs
    – Tom J Nowell
    Commented Apr 29, 2015 at 15:35
  • No offense - but caching doesn't change the fact that it's not scaling well ;-)
    – jjarolim
    Commented May 4, 2015 at 14:58
0

This answer is going to integrate the one from jjarolim

The correct whole query is

SELECT DATE(post_date) AS date, concat(post_title) titles, COUNT(*) AS count
FROM  wp_posts 
WHERE post_status = 'publish'
AND post_parent = 0
AND post_type = 'post'
GROUP BY DATE(post_date)
ORDER BY DATE(post_date)
3
  • Please remember to accept your own answer when the restriction is lifted :-) Commented May 8, 2015 at 10:16
  • 1
    @PieterGoosen I would prefer to accept one of the 2 others if they make a small correction.. The one from nowell is also very nice, but it's not separated by days.. I upvoted them both.
    – Revious
    Commented May 8, 2015 at 10:19
  • 1
    Ah, I see. Good sportmanship ;-). Commented May 8, 2015 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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