$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>posts - View the number of entries recorded per day|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

posts - View the number of entries recorded per day

matteradmin11PV0评论

I want to display a chart of the number of articles published from a post_type within a specified time period (e.g. 30 days). Example : 1/1/2019 (1) 1/2/2019 (15) 1/3/2019 (0) 1/4/2019 (6) 1/5/2019 (0) 1/6/2019 (3) 1/7/2019 (7)

I want to display a chart of the number of articles published from a post_type within a specified time period (e.g. 30 days). Example : 1/1/2019 (1) 1/2/2019 (15) 1/3/2019 (0) 1/4/2019 (6) 1/5/2019 (0) 1/6/2019 (3) 1/7/2019 (7)

Share Improve this question asked Mar 12, 2019 at 21:50 Milad AbbasiMilad Abbasi 213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The easiest way is by using custom MySQL query with WPDB class.

global $wpdb;
$table = $wpdb->prefix . 'posts';
$sql = "SELECT DATE(post_date) AS date, COUNT(ID) AS count 
    FROM {$table} WHERE post_type = 'my_post_type' AND post_status = 'publish' GROUP BY DATE(post_date)";
$rows = $wpdb->get_results($sql);

Or if you want to prevent SQL injection, you can use prepared statement like this:

global $wpdb;
$sql = $wpdb->prepare(
    "SELECT DATE(post_date) AS date, COUNT(ID) AS count 
    FROM %s WHERE post_type = %s AND post_status = 'publish' GROUP BY DATE(post_date)",
    array(
        $wpdb->prefix . 'posts',
        'my_post_type'
    )
);
$rows = $wpdb->get_results($sql);

Then you can iterate the $rows to show the data.

foreach ($rows as $row) {
    echo $row->date . ' -- ' . $row->count;
}
Post a comment

comment list (0)

  1. No comments so far