Skip to content

Instantly share code, notes, and snippets.

@artlung
Last active November 9, 2023 02:25
Show Gist options
  • Save artlung/3878fe702a2f59bc29c7a34b29015471 to your computer and use it in GitHub Desktop.
Save artlung/3878fe702a2f59bc29c7a34b29015471 to your computer and use it in GitHub Desktop.
This was because I wanted a sentence on my about page that would read: "This blog has 8,586 posts, 2,353 comments, 5,936 embedded images, and 3,258 outbound links." https://artlung.com/about/ -- I use a cron to run the CLI prefixed files to update the outbound links and images. For your use, possibly.
<?php
$current_dir = dirname(__FILE__);
// load wordpress functions
require_once( $current_dir . '/../../../wp-load.php');
$meta_key = 'post_image_count';
// TODO
// TODO post_link_count
// get posts without the post_word_count key
$args = [
// type post
'post_type' => ['post', 'page'],
'posts_per_page' => 200,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'ASC',
'meta_query' => [
[
'key' => $meta_key,
'compare' => 'NOT EXISTS',
],
],
];
// https://developer.wordpress.org/reference/functions/get_posts/
$posts = get_posts($args);
foreach ($posts as $post) {
// get the finalized content
$content = get_the_content();
// parse the content for img tags
$dom = new RoanokeDOMDocument();
$dom->loadHTML($content);
$images = $dom->getElementsByTagName('img');
$image_count = $images->length;
$meta_value = $image_count;
// if already has key, continue
if (get_post_meta($post->ID, $meta_key, true)) {
printf("Post %s already has meta key %s\n", $post->ID, $meta_key);
continue;
}
printf("DONE: Post %s meta key %s will be set to %s\n", $post->ID, $meta_key, $meta_value);
update_post_meta($post->ID, $meta_key, $meta_value);
}
add_shortcode('post_stats_sentence', function() {
return Roanoke::postStatsSentence();
});
<?php
/**
* class Roanoke
*/
class Roanoke {
// Note, this depends on every post having a post_image_count and post_link_count
public static function postStatsSentence() {
$number_of_published_posts = wp_count_posts()->publish;
$number_of_published_comments = wp_count_comments()->approved;
$number_of_embedded_images = self::totalMetaValue('post_image_count');
$number_of_outbound_links = self::totalMetaValue('post_link_count');
$number_of_published_posts = number_format($number_of_published_posts);
$number_of_published_comments = number_format($number_of_published_comments);
$number_of_embedded_images = number_format($number_of_embedded_images);
$number_of_outbound_links = number_format($number_of_outbound_links);
$sentence = sprintf('This blog has %s posts, %s comments, %s embedded images, and %s outbound links.',
$number_of_published_posts,
$number_of_published_comments,
$number_of_embedded_images,
$number_of_outbound_links
);
return $sentence;
}
private static function totalMetaValue( string $string ) {
global $wpdb;
$sql = "SELECT SUM(meta_value) FROM {$wpdb->prefix}postmeta WHERE meta_key = '{$string}'";
$result = $wpdb->get_var($sql);
return $result;
}
}
<?php
class RoanokeDOMDocument extends DOMDocument
{
/**
* When specifying we want to to be sure the output does not contain the doctype, html, or body tags.
* @param $source
* @param $options
*
* @return void
*/
function loadHTML( $source, $options = 0 ) {
// https://stackoverflow.com/questions/4879946/how-to-savehtml-of-domdocument-without-html-wrapper
$options = $options ?: LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD;
parent::loadHTML( $source, $options );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment