Using WordPress ‘metadata_lazyloader_queued_objects’ PHP action

The metadata_lazyloader_queued_objects WordPress action fires after objects are added to the metadata lazy-load queue.

Usage

add_action('metadata_lazyloader_queued_objects', 'my_custom_function', 10, 3);

function my_custom_function($object_ids, $object_type, $lazyloader) {
  // Your custom code here
}

Parameters

  • $object_ids (array) – Array of object IDs.
  • $object_type (string) – Type of object being queued.
  • $lazyloader (WP_Metadata_Lazyloader) – The lazy-loader object.

More information

See WordPress Developer Resources: metadata_lazyloader_queued_objects

Examples

Log queued object IDs

Log the queued object IDs to a file.

add_action('metadata_lazyloader_queued_objects', 'log_queued_object_ids', 10, 3);

function log_queued_object_ids($object_ids, $object_type, $lazyloader) {
  // Convert object IDs array to a string
  $ids_string = implode(', ', $object_ids);

  // Log to a file
  error_log("Queued Object IDs for {$object_type}: {$ids_string}");
}

Add custom metadata

Add custom metadata to queued objects.

add_action('metadata_lazyloader_queued_objects', 'add_custom_metadata', 10, 3);

function add_custom_metadata($object_ids, $object_type, $lazyloader) {
  // Check if the object type is 'post'
  if ($object_type === 'post') {
    // Add custom metadata to each queued post
    foreach ($object_ids as $post_id) {
      add_post_meta($post_id, 'my_custom_meta_key', 'my_custom_meta_value');
    }
  }
}

Modify post titles

Modify post titles of queued objects.

add_action('metadata_lazyloader_queued_objects', 'modify_post_titles', 10, 3);

function modify_post_titles($object_ids, $object_type, $lazyloader) {
  if ($object_type === 'post') {
    foreach ($object_ids as $post_id) {
      $post = get_post($post_id);
      $post->post_title = 'Modified: ' . $post->post_title;
      wp_update_post($post);
    }
  }
}

Remove specific metadata

Remove a specific metadata from queued objects.

add_action('metadata_lazyloader_queued_objects', 'remove_specific_metadata', 10, 3);

function remove_specific_metadata($object_ids, $object_type, $lazyloader) {
  if ($object_type === 'post') {
    foreach ($object_ids as $post_id) {
      delete_post_meta($post_id, 'unwanted_meta_key');
    }
  }
}

Count queued objects

Count the number of queued objects and store the result in an option.

add_action('metadata_lazyloader_queued_objects', 'count_queued_objects', 10, 3);

function count_queued_objects($object_ids, $object_type, $lazyloader) {
  // Get the current count from the option
  $current_count = get_option('my_queued_object_count', 0);

  // Update the count
  $new_count = $current_count + count($object_ids);
  update_option('my_queued_object_count', $new_count);
}