Generate a related posts plugin
Related posts at the bottom of an article keep readers on the site. The plugins that do this well (YARPP, Contextual Related Posts) use complex SQL full-text matching or external search engines. Fine for big sites. Overkill for most.
For a typical blog, a scoring function that combines taxonomy overlap, tag overlap, and recency produces solid related posts without heavy infrastructure. The scoring is cheap, and we cache the top 5 per post so serving has near-zero cost.
Why generate it instead of installing an existing plugin?
YARPP works but its SQL is heavy on sites with >10k posts. Jetpack Related Posts requires Jetpack and sends data to WordPress.com. Jetpack is a big install for one feature.
A generated plugin scores each candidate post based on shared categories (weight 3), shared tags (weight 1), and recency bonus (newer wins ties). It computes the top 5 once per post (on save or via a cron backfill) and stores the ids in post meta. Rendering is a simple WP_Query by IDs.
Result: a related-posts block that loads in sub-10ms because there is no runtime scoring. Re-scoring happens when a post changes or its taxonomy changes.
Example prompt
This is the kind of description that generates this plugin. You can start from it and tweak whatever you need before hitting generate.
Plugin name: Acme Related Posts
On save_post (post type = post):
- Score all other published posts: 3 per shared category, 1 per shared tag, +0.5 bonus if published within 6 months.
- Store the top 5 post ids in post meta acme_related_ids.
Fallback: if taxonomy overlap yields nothing, use most recent 5 in the same category.
Shortcode [acme_related] and block acme/related renders a list from acme_related_ids using a single WP_Query by post__in.
Admin: "Rebuild all related ids" button (Tools page) that chunks through all posts, 50 at a time via WP Cron.
Recompute trigger: save_post, set_object_terms. Invalidate downstream posts that previously included this one.What the generated plugin typically includes
- Scoring function combining categories, tags, and recency
- Cached top-5 ids per post stored in post meta
- Block + shortcode render from cached ids (no runtime scoring)
- Invalidation on save_post and term updates
- Admin rebuild-all action with chunked cron processing
Scoring weights, post types considered, and cache size (top 5 vs 10) are defined in the prompt. For multilingual sites, describe the language-matching rule so related posts stay in the same language.
Frequently asked questions
What if my site has 100k posts?
The scoring is O(n) per save, so ~100ms for 100k posts. Still fine for save actions because it runs once. The rebuild-all cron handles it in chunks so nothing times out.
Can I exclude certain tags from scoring?
Yes. Describe excluded tags in the prompt (e.g., "internal" or "landing-page") and the scorer skips them.