Optimize Custom WordPress Queries

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:


// 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…





keywords: wp_query only get ids, wp_query cache_results

Tagged with: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *