Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
wpdb道場第一回目
<?php
// これでWordPress関数がほとんど使えるようになる!
include 'wp-load.php';
// オプションを10件取得
$options = $wpdb->get_results("SELECT * FROM {$wpdb->options} LIMIT 10");
// 公開済み投稿の本文の長さを全部足す
$query = <<<SQL
SELECT SUM(CHAR_LENGTH(post_content))
FROM {$wpdb->posts}
WHERE post_status = 'publish'
AND post_type = 'post'
SQL;
$total = $wpdb->get_var($query);
// 公開済み投稿の本文の長さを平均値を出す
$query = <<<SQL
SELECT AVG(CHAR_LENGTH(post_content))
FROM {$wpdb->posts}
WHERE post_status = 'publish'
AND post_type = 'post'
SQL;
$average = $wpdb->get_var($query);
var_dump($average);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment