Archived topic
Cache WP Show Posts Random
13 replies · Started by epickenyan on March 3, 2019
Hi. I saw some web hosts like WPEngine have an inbuilt system that caches random posts for some time, e.g. 10 minutes, so as to improve site speed. Can this be achieved with WP show Posts plugin? I want to show random posts at intervals in the sidebar.
Hi there,
Unfortunately WP Show Posts doesn't this option currently.
I believe it's on the radar though.
For more questions regarding to WP Show Posts, please use its own forum as it's a separate plugin:
https://wpshowposts.com/support/area/pro-support/
https://wordpress.org/support/plugin/wp-show-posts/
Thanks!
I used this solution and then hooked it after sidebar. Working perfectly
Awesome!
1. Hi. Is there a way to hook such a code provided in the post in this comment to a footer widget? I want to show a subscription box in one widget and the related posts in the second widget. The PHP code does not work on the front end of WordPress.
2. Since the above two widgets are the only ones I have in the entire site, would you recommend I put them in the Sidebar or remove sidebar altogether and put them in the footer? Someone in the Facebook group recommended I remove the sidebar.
Hi there,
1. You would have to add it to a shortcode: https://docs.generatepress.com/article/creating-a-shortcode/
2. It depends on your preference/design. A sidebar widget might be more visible than a footer widget, but it might not work with your design.
1. I have asked over at StackExchange how to get the function to create the shortcode. But if you know how, you can assist.
2. Thanks. I think the footer works best. I can reduce the whole site width to 1080px and the single post width to 960px.
You could add your code into the shortcode function we provide in that article. Then you should be able to use the shortcode and it will output the posts.
I did, but I was getting some errors (syntax etc).
Can you share the error you were getting and the code you were adding?
I tried this code
add_shortcode( 'my_shortcode', 'tu_my_shortcode' );
function tu_my_shortcode() {
ob_start();
<?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
$randargs = array('orderby' => 'rand', 'numberposts' => 20);
$special_query_results = get_posts($randargs);
set_transient( 'special_query_results', $special_query_results, 60*60*12 );
}
// Use the data like you would have normally...
$randomposts = get_transient( 'special_query_results' );
$randkey = array_rand( $randomposts, 6 );
$sixposts[0] = $randomposts[$randkey[0]];
$sixposts[1] = $randomposts[$randkey[1]];
$sixposts[2] = $randomposts[$randkey[2]];
$sixposts[3] = $randomposts[$randkey[3]];
$sixposts[4] = $randomposts[$randkey[4]];
$sixposts[5] = $randomposts[$randkey[5]];
global $post;
foreach( $sixposts as $post ) : setup_postdata($post); ?>
<div class="gridcontainer">
<div class="gridthumb"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
<div class="gridcontent">
<div class="gridtext"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
</div>
</div>
<?php endforeach; ?>
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
If I remove the opening php in the code, three errors at the top disappear, but I have an error at the bottom where the closing '}' is.
Try this:
add_shortcode( 'my_shortcode', 'tu_my_shortcode' );
function tu_my_shortcode() {
ob_start();
// 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
$randargs = array('orderby' => 'rand', 'numberposts' => 20);
$special_query_results = get_posts($randargs);
set_transient( 'special_query_results', $special_query_results, 60*60*12 );
}
// Use the data like you would have normally...
$randomposts = get_transient( 'special_query_results' );
$randkey = array_rand( $randomposts, 6 );
$sixposts[0] = $randomposts[$randkey[0]];
$sixposts[1] = $randomposts[$randkey[1]];
$sixposts[2] = $randomposts[$randkey[2]];
$sixposts[3] = $randomposts[$randkey[3]];
$sixposts[4] = $randomposts[$randkey[4]];
$sixposts[5] = $randomposts[$randkey[5]];
global $post;
foreach( $sixposts as $post ) : setup_postdata($post); ?>
<div class="gridcontainer">
<div class="gridthumb"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
<div class="gridcontent">
<div class="gridtext"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
</div>
</div>
<?php endforeach;
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
You'll want to replace my_shortcode with the shortcode name you want to use in your content.
Thanks a lot, Tom. It worked! I decided to just keep the footer and remove the sidebar.
Glad I could help :)