Pulling this php pattern out of a larger project — might be useful on its own.
Fetch published posts by meta key/value — minimal wrapper.
<?php
namespace Acme\Queries;
use WP_Query;
function posts_by_meta(string $key, string $value, int $limit = 20): array {
$query = new WP_Query([
'post_status' => 'publish',
'posts_per_page' => max(1, min(100, $limit)),
'no_found_rows' => true,
'meta_query' => [[
'key' => $key,
'value' => $value,
'compare' => '=',
]],
]);
return $query->posts;
}
Generate a URL-safe 10-char slug.
<?php
function codelag_random_slug(int $length = 10): string {
$alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789';
$max = strlen($alphabet) - 1;
$out = '';
for ($i = 0; $i < $length; $i++) {
$out .= $alphabet[random_int(0, $max)];
}
return $out;
}
Used it in production, works on my machine™.
Developer Discussions