Alright. This is the source code of the wpp_get_mostpopular() function:
/**
* Template tag - gets popular posts.
*
* @since 2.0.3
* @param mixed args
*/
function wpp_get_mostpopular($args = NULL) {
$shortcode = '[wpp';
if ( is_null( $args ) ) {
$shortcode .= ']';
} else {
if( is_array( $args ) ){
$atts = '';
foreach( $args as $key => $arg ){
if (
is_array( $arg )
&& ( 'post_type' == $key || 'cat' == $key || 'term_id' == $key || 'pid' == $key || 'author' == $key )
) {
$arg = array_filter( $arg, 'is_int' );
$arg = join( ',', $arg );
}
$atts .= ' ' . $key . '="' . htmlspecialchars($arg, ENT_QUOTES, $encoding = ini_get("default_charset"), false) . '"';
}
} else {
$atts = trim( str_replace( "&", " ", $args ) );
}
$shortcode .= ' ' . $atts . ' php=true]';
}
echo do_shortcode( $shortcode );
}
If you notice, it’s just a wrapper that calls the [wpp]
shortcode.
Here’s the source code that registers and processes the [wpp]
shortcode:
/**
* WPP shortcode handler.
*
* @since 1.4.0
* @param array $atts User defined attributes in shortcode tag
* @return string
*/
public function wpp_shortcode( $atts = null ) {
// Previous lines of code removed for brevity!
$cached = false;
// Return cached results
if ( $this->admin_options['tools']['cache']['active'] ) {
$key = md5( json_encode($shortcode_ops) );
$popular_posts = WPP_Cache::get( $key );
if ( false === $popular_posts ) {
$popular_posts = new WPP_Query( $shortcode_ops );
$time_value = $this->admin_options['tools']['cache']['interval']['value']; // eg. 5
$time_unit = $this->admin_options['tools']['cache']['interval']['time']; // eg. 'minute'
WPP_Cache::set(
$key,
$popular_posts,
$time_value,
$time_unit
);
}
$cached = true;
} // Get popular posts
else {
$popular_posts = new WPP_Query( $shortcode_ops );
}
$output = new WPP_Output( $popular_posts->get_posts(), $shortcode_ops );
$shortcode_content .= $output->get_output();
return $shortcode_content;
}
If Data Caching is enabled, then the plugin check if there’s a transient set already. If so, that’s what the plugin will use to render the popular posts. If there isn’t a transient set, then WPP does a new query and store its results in a transient (WPP_Cache::set(...)
). In case you’re curious, here’s the source code of the WPP_Cache class.
As you can see, the plugin is doing exactly the same thing you’re trying to do here. Don’t see how your code is any different (in terms of logic) from what WPP already does.
If you still prefer doing it your way and don’t mind duplicating some code, this is what you need:
function pop_func( $atts ) {
$args = array(
'post_type' => 'post',
'range' => 'all',
);
if ( ! $pop = get_transient( 'cache-popular' ) ) {
$shortcode = '[wpp';
if ( is_null( $args ) ) {
$shortcode .= ']';
} else {
if( is_array( $args ) ){
$atts = '';
foreach( $args as $key => $arg ){
if (
is_array( $arg )
&& ( 'post_type' == $key || 'cat' == $key || 'term_id' == $key || 'pid' == $key || 'author' == $key )
) {
$arg = array_filter( $arg, 'is_int' );
$arg = join( ',', $arg );
}
$atts .= ' ' . $key . '="' . htmlspecialchars($arg, ENT_QUOTES, ini_get("default_charset"), false) . '"';
}
} else {
$atts = trim( str_replace( "&", " ", $args ) );
}
$shortcode .= ' ' . $atts . ' php=true]';
}
$pop = do_shortcode( $shortcode );
set_transient( 'cache-popular', $pop, 8 * HOUR_IN_SECONDS );
}
return $pop;
}
add_shortcode( 'pop', 'pop_func' );