Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Created February 23, 2017 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/518b143d9b84e0c3b0f4e17b9ddb3615 to your computer and use it in GitHub Desktop.
Save damiencarbery/518b143d9b84e0c3b0f4e17b9ddb3615 to your computer and use it in GitHub Desktop.
Quick comparison of using wp_list_pluck() and "fields"=>'ids" with WP_Query
<?php
// Restrict who can access this script.
$permitted_ips = array('12.34.56.78');
if (in_array($_SERVER['REMOTE_ADDR'], $permitted_ips) == false) {
header('HTTP/1.0 403 Forbidden');
die();
}
?>
<!DOCTYPE html>
<!-- $Id: update-transients.php 4163 2016-12-16 11:36:52Z damien $ -->
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>Compare fields vs wp_list_pluck</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<h1>Compare fields vs wp_list_pluck</h1>
<?php
define('WP_USE_THEMES', false);
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
// Get IDs of all products.
$time_start = microtime(true);
$args = array('post_type'=>'product', 'posts_per_page' => -1, 'fields' => 'ids', 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false);
$products_query = new WP_Query($args);
$product_ids = array();
$product_ids = $products_query->posts;
wp_reset_postdata();
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<p>fields process Time: {$time} seconds.</p>";
// Get IDs of all products.
$time_start = microtime(true);
$args = array('post_type'=>'product', 'posts_per_page' => -1, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false);
$products_query = new WP_Query($args);
$product_ids = wp_list_pluck($products_query->posts, 'ID');
wp_reset_postdata();
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<p>wp_list_pluck process Time: {$time} seconds.</p>";
// Repeat
// Get IDs of all products.
$time_start = microtime(true);
$args = array('post_type'=>'product', 'posts_per_page' => -1, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false);
$products_query = new WP_Query($args);
$product_ids = wp_list_pluck($products_query->posts, 'ID');
wp_reset_postdata();
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<p>wp_list_pluck process Time: {$time} seconds.</p>";
// Get IDs of all products.
$time_start = microtime(true);
$args = array('post_type'=>'product', 'posts_per_page' => -1, 'fields' => 'ids', 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false);
$products_query = new WP_Query($args);
$product_ids = array();
$product_ids = $products_query->posts;
wp_reset_postdata();
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<p>fields process Time: {$time} seconds.</p>";
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment