• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

Optimize Custom WordPress Queries

November 20, 2012 by Webhead

A couple things that may optimize your custom wordpress queries.  First there is a parameter called ‘fields’ where  you can specify that you only want the ID field back.  Something like below:

$some_query = new WP_Query( array( ‘fields’ => ‘ids’ ) );

An even more significant optimization is by caching your data and using WordPress’ Transient API like so:

<?php

// Get any existing copy of our transient data

if ( false === ( $special_query_results = get_transient( ‘special_query_results’ ) ) ) {

// It wasn’t there, so regenerate the data and save the transient

$special_query_results = new WP_Query( ‘cat=5&order=random&tag=tech&post_meta_key=thumbnail’ );

set_transient( ‘special_query_results’, $special_query_results );

}

// Use the data like you would have normally…

?>

 

sources:

http://www.wpbeginner.com/wp-tutorials/speed-up-your-wordpress-by-caching-custom-queries-using-transients-api/
http://wordpress-hackers.1065353.n5.nabble.com/Using-WP-Query-to-get-posts-ids-td23952.html
http://codex.wordpress.org/Transients_API

keywords: wp_query only get ids, wp_query cache_results

Filed Under: Coding Tagged With: optimize, php, wordpress

Primary Sidebar

Topics

apache apple block editor chrome cms css debug eCommerce embed firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery linux localization macOS mac os x ms sql mysql open source optimize php php 7.2 rest api seo svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools
  • Uncategorized