Search code examples
wordpressapitransient

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
// Creating the widget
class wpb_box extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_box',
// Widget name will appear in UI
__('ابزارک اختصاصی باکس مطالب خبرخوان', 'bigtheme'),
// Widget description
array( 'description' => __( 'ابزارک نمایش باکس مطالب از سایت های مختلف توسط آدرس خوراک یا فید سایت', 'bigtheme' ), )
);
add_action('save_post', array( $this, 'delete_query_caches') );
}
// Creating widget front-end
// This is where the action happens
/**
     * Delete transients
     */
    function delete_query_caches( $post_id ){
        if( !isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;

        $categories = wp_get_post_terms( $post_id, 'category' );

        if( $categories && ! is_WP_Error( $categories ) ) {
            foreach( $categories as $cat ) {
                delete_transient('foo_featured_posts_' . $category);
            }
        }
    }
public function widget( $args, $instance ) {
        $name = apply_filters( 'widget_title', $instance['name'] );
        $category = apply_filters( 'widget_title', $instance['category'] );
        $id = apply_filters( 'widget_title', $instance['id'] );
$link = apply_filters( 'widget_title', $instance['link'] );
        $display = apply_filters( 'widget_title', $instance['display'] );
        $color = apply_filters( 'widget_title', $instance['color'] );
// This is where you run the code and display the output
?>
<div class="col-sm-6 col-xs-12 padding5">
                <div class="article">
                        <div class="title">     
                            <h3><a href="<?php echo $link ?>" target="_blank"><?php echo $name ?></a></h3>
                            <div class="clear"></div>
                            <div class="line"></div>
                    </div>

                               <?php
if ( false === ( $slider = get_transient( 'foo_featured_posts_' . $category ) ) ) {

     $slider = new WP_Query(array(
            'post_status' =>'publish',
            'post_type' =>'post',
            'cat' =>''.$category.'',
            'posts_per_page' =>'9'      
        ));

    // Make a new query cache for 1 week
    set_transient( 'foo_featured_posts_' . $category, $slider, 10 * MINUTE_IN_SECONDS );

}
if( !$slider->have_posts() ) return; ?>
<ul>
<?php while( $slider->have_posts() ) : $slider->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; $slider->rewind_posts(); ?>
</ul>
<?php wp_reset_postdata(); ?>
<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>
<?php
echo $args['after_widget'];
}
public function form( $instance ) {
        $name   = ( isset( $instance[ 'name' ] ) ) ? $instance[ 'name' ] : '';
        $category   = ( isset( $instance[ 'category' ] ) ) ? $instance[ 'category' ] : '';
$link   = ( isset( $instance[ 'link' ] ) ) ? $instance[ 'link' ] : '';
        $color  = ( isset( $instance[ 'color' ] ) ) ? $instance[ 'color' ] : '';
        $id     = ( isset( $instance[ 'id' ] ) ) ? $instance[ 'id' ] : '';
        $display    = ( isset( $instance[ 'display' ] ) ) ? $instance[ 'display' ] : '';
?>
            <p>
                <label for="<?php echo $this->get_field_id( 'color' ); ?>"><?php _e( 'رنگ باکس مطالب:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" placeholder="مثال : #CCC , #dd3333 , black , blue" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'عنوان باکس:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" type="text" value="<?php echo esc_attr( $name ); ?>" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'id' ); ?>"><?php _e( 'آی دی دسته بندی' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'id' ); ?>" name="<?php echo $this->get_field_name( 'id' ); ?>" type="text" value="<?php echo esc_attr( $id ); ?>" />
            </p>
            <select id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" class="widefat" style="width:100%;">
            <?php foreach(get_terms('category','parent=0&hide_empty=0') as $term) { ?>
            <option <?php selected( $instance['category'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
            <?php } ?>      
        </select>
<p>
                <label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'لینک آرشیو' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" />
            </p>

            <p>
                <label><?php _e( 'نمایش توضیحات مطالب' ); ?></label>
                <select class="widefat" id="<?php echo $this->get_field_id( 'display' ); ?>" name="<?php echo $this->get_field_name( 'display' ); ?>">
                    <option <?php selected( $instance['display'], 'block'); ?> value="block">بله</option> 
                    <option <?php selected( $instance['display'], 'none'); ?> value="none">خیر</option> 
                </select>
            </p>

<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['name'] = ( ! empty( $new_instance['name'] ) ) ? strip_tags( $new_instance['name'] ) : '';
        $instance['category'] = ( ! empty( $new_instance['category'] ) ) ? strip_tags( $new_instance['category'] ) : '';
$instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';
        $instance['link2'] = ( ! empty( $new_instance['link2'] ) ) ? strip_tags( $new_instance['link2'] ) : '';
        $instance['id'] = ( ! empty( $new_instance['id'] ) ) ? strip_tags( $new_instance['id'] ) : '';
        $instance['link3'] = ( ! empty( $new_instance['link3'] ) ) ? strip_tags( $new_instance['link3'] ) : '';
        $instance['link4'] = ( ! empty( $new_instance['link4'] ) ) ? strip_tags( $new_instance['link4'] ) : '';
        $instance['link5'] = ( ! empty( $new_instance['link5'] ) ) ? strip_tags( $new_instance['link5'] ) : '';
        $instance['color'] = ( ! empty( $new_instance['color'] ) ) ? strip_tags( $new_instance['color'] ) : '';
        $instance['display'] = ( ! empty( $new_instance['display'] ) ) ? strip_tags( $new_instance['display'] ) : '';
        $instance['source'] = ( ! empty( $new_instance['source'] ) ) ? strip_tags( $new_instance['source'] ) : '';
        $instance['time'] = ( ! empty( $new_instance['time'] ) ) ? strip_tags( $new_instance['time'] ) : '';
        return $instance;
    }
} // Class wpb_box ends here
// Register and load the widget
function wpb_box() {
register_widget( 'wpb_box' );
}
add_action( 'widgets_init', 'wpb_box' );
?>

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


Solution

  • You are creating just one transient: foo_featured_posts.

    set_transient( 'foo_featured_posts', $featured, 10 * MINUTE_IN_SECONDS );
    

    If you want to display different things for different categories you should create a transient for each category: foo_featured_posts_cat_1, foo_featured_posts_cat_2, etc.

    set_transient( 'foo_featured_posts_' . $category, $featured, 10 * MINUTE_IN_SECONDS );