Skip to content

Instantly share code, notes, and snippets.

@salcode
Created December 21, 2015 18:05
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 salcode/24a6e02ce48218e14011 to your computer and use it in GitHub Desktop.
Save salcode/24a6e02ce48218e14011 to your computer and use it in GitHub Desktop.
<?php
add_shortcode( 'query_results', 'fe_query_results' );
add_action( 'save_post', 'fe_query_results_delete_transient' );
function fe_query_results( $atts ) {
global $post;
$transient_key = 'fe_query_results_transient_key';
$transient_time = 24 * HOUR_IN_SECONDS;
// define attributes and their defaults
extract( shortcode_atts( array (
'type' =&gt; 'post',
'order' =&gt; 'date',
'orderby' =&gt; 'title',
'posts_per_page' =&gt; 200,
), $atts ) );
// return transient if it exists
if ( false !== ( $output = get_transient( $transient_key ) ) ) {
return $output;
}
$output = '';
$args = array(
'post_type' =&gt; $type,
'order' =&gt; $order,
'orderby' =&gt; $orderby,
'posts_per_page' =&gt; $posts_per_page,
// re-enable if you need pagination
'no_found_rows' =&gt; true,
// re-enable if you use post meta in your output
'update_post_meta_cache'=&gt; false,
// re-enable if you use terms in your output
'update_post_term_cache'=&gt; false,
);
$fe_query= new WP_Query( $args );
// etc, etc...................
@salcode
Copy link
Author

salcode commented Dec 21, 2015

In additions to my comments in the blog post, I noticed you're using extract() in your code. It is generally recommended to avoid using this function.

This ticket discusses the use of extract() in WordPress core and its removal https://core.trac.wordpress.org/ticket/22400

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment