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:

<?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

Tagged with: , ,

Leave a Reply

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

*