{"id":904,"date":"2012-11-20T12:37:01","date_gmt":"2012-11-20T22:37:01","guid":{"rendered":"http:\/\/mymonkeydo.com\/?p=904"},"modified":"2012-11-20T12:37:01","modified_gmt":"2012-11-20T22:37:01","slug":"optimize-custom-wordpress-queries","status":"publish","type":"post","link":"https:\/\/mymonkeydo.com\/optimize-custom-wordpress-queries\/","title":{"rendered":"Optimize Custom WordPress Queries"},"content":{"rendered":"
A couple things that may optimize your custom wordpress queries. \u00a0First there is a parameter called ‘fields’ where \u00a0you can specify that you only want the ID field back. \u00a0Something like below:<\/p>\n
$some_query = new WP_Query( array( ‘fields’ => ‘ids’ ) );<\/p>\n
An even more significant optimization is by caching your data and using WordPress’ Transient API like so:<\/p>\n
<?php<\/p>\n
\/\/ Get any existing copy of our transient data<\/p>\n
if ( false === ( $special_query_results = get_transient( ‘special_query_results’ ) ) ) {<\/p>\n
\/\/ It wasn’t there, so regenerate the data and save the transient<\/p>\n
$special_query_results = new WP_Query( ‘cat=5&order=random&tag=tech&post_meta_key=thumbnail’ );<\/p>\n
set_transient( ‘special_query_results’, $special_query_results );<\/p>\n
}<\/p>\n
\/\/ Use the data like you would have normally…<\/p>\n
?><\/p>\n
<\/p>\n
sources:<\/p>\n
http:\/\/www.wpbeginner.com\/wp-tutorials\/speed-up-your-wordpress-by-caching-custom-queries-using-transients-api\/ keywords:\u00a0wp_query only get ids,\u00a0wp_query cache_results<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[2],"tags":[53,54,64],"yoast_head":"\n
\n<\/a>http:\/\/wordpress-hackers.1065353.n5.nabble.com\/Using-WP-Query-to-get-posts-ids-td23952.html
\n<\/a>http:\/\/codex.wordpress.org\/Transients_API<\/a><\/p>\n