-
-
Save salcode/24a6e02ce48218e14011 to your computer and use it in GitHub Desktop.
Code from Paul McRobb's comment at http://salferrarello.com/wordpress-shortcode-wp_query/#comment-14180
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' => 'post', | |
'order' => 'date', | |
'orderby' => 'title', | |
'posts_per_page' => 200, | |
), $atts ) ); | |
// return transient if it exists | |
if ( false !== ( $output = get_transient( $transient_key ) ) ) { | |
return $output; | |
} | |
$output = ''; | |
$args = array( | |
'post_type' => $type, | |
'order' => $order, | |
'orderby' => $orderby, | |
'posts_per_page' => $posts_per_page, | |
// re-enable if you need pagination | |
'no_found_rows' => true, | |
// re-enable if you use post meta in your output | |
'update_post_meta_cache'=> false, | |
// re-enable if you use terms in your output | |
'update_post_term_cache'=> false, | |
); | |
$fe_query= new WP_Query( $args ); | |
// etc, etc................... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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