using Transient API in WordPress

i want to use Transient API to make faster website loading.i use this code in widget but in all widgets just display last posts not specific category’s posts

<?php 
// Check for transient. If none, then execute WP_Query
if ( false === ( $featured = get_transient( 'foo_featured_posts' ) ) ) {

      $featured = new WP_Query(
       array(
        'cat' => ''.$link1.'',
        'posts_per_page' => 9
       ));

    // Put the results in a transient. Expire after 10 minutes.
    set_transient( 'foo_featured_posts', $featured, 10 * MINUTE_IN_SECONDS );
} ?>

<?php if ( $featured->have_posts() ) : ?>

   <?php while ( $featured->have_posts() ) : $featured->the_post(); ?>
      <li>
<a href="<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a>
<div style="display:<?php echo $display;?>" class="tooltiptext hidden-xs"><?php the_excerpt(); ?></div>
</li>
   <?php endwhile; ?>

   <?php else: ?>
         // no featured posts found
<?php endif; ?>

<?php wp_reset_postdata(); ?>
</ul>
<div class="list"><a href="<?php echo $link ?>" target="_blank">مشاهده آرشیو کامل</a><a href="<?php echo $link ?>/feed" target="_blank"><img src="<?php bloginfo('template_url'); ?>/images/rss.png" width="18" height="18" alt="خوراک سایت" ></a></div>
            </div>
                </div>

but away is this code correct for using transient api or not?!

Read more here: using Transient API in WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *