HomeMethod

Knowledge

Web VitalsTechniquesSnippetsAnswersImpactsBottlenecks

Inefficient use of custom fields

  • Database

Problem Summary

Custom fields (post meta) store additional data per post. Inefficient use includes storing large data, not using indexes, or making separate queries for each post in loops.

Performance Impact

  • Slow queries: Meta queries are often slow
  • N+1 problems: Separate query per post
  • Database bloat: Large meta values
  • Memory issues: Storing too much in meta

How to Detect

  1. Query Monitor: Shows meta queries

  2. PHP profiling: Slow meta lookups
  3. Database analysis: Check wp_postmeta size

Solutions

1. Use get_post_meta() Once

Code
// Bad - query per postforeach ( as ) {   = get_post_meta(->ID, 'key', true);}// Good - get all at once = wp_list_pluck(, 'ID'); = get_post_meta(, 'key', true);

2. Use Custom Post Types

Instead of meta, use separate post types for complex data.

3>Index Meta Fields

Code
<?php
add_action('added_post_meta', 'add_meta_index');function add_meta_index() {  global ;  ->query("ALTER TABLE ->postmeta ADD INDEX meta_key (meta_key(191))");}

4>Store Reference, Not Data

Store IDs pointing to other tables instead of serialized data.

Further Reading

Support our family now