WordPress posts per page WP_Query

I’m trying to limit the posts per page using WP_Query.
Please see the code below and see why it doesn’t work for me:

    public function widget( $args, $instance ) {
        // outputs the content of the widget
            global $wpdb,$post;

            extract( $args );
            $title = apply_filters( 'widget_title', $instance['title'] );
            $number = intval($instance['number'] );
            if($number<=0){
                $number = 3; // default  = 3;
            }

            echo $before_widget;
            if ( ! empty( $title ) )
                echo $before_title . $title . $after_title;

               $args = array( 'posts_per_page' => $number );
                if($cats>0){
                    $args['category__in'] =  array($cats);
                }
                $args['post__not_in'] = array($post->ID);
                $args['orderby'] = 'post_date';
                $args['order'] = 'DESC';
                 $args['post_status'] = 'publish';
                 $args['nopaging'] = true;                
                 if(st_is_wpml()){
                     $args['sippress_filters'] = true;
                    $args['language'] = get_bloginfo('language');
                 }
                 $new_query = new WP_Query($args);
                 $posts =  $new_query->posts;

            /*
             $args = array(
                'numberposts' => $number,
                'orderby' => 'post_date',
                'order' => 'DESC',
                'exclude' => array($post->ID) ,
              //  'post_type' => $post->post_type,
                'post_status' => 'publish'
              );

                $posts = wp_get_recent_posts( $args ,'OBJECT');
                */

            if($posts){ ?>
            <ul class="po_re_container">
                <?php foreach($posts as $post){ setup_postdata($post); ?>

                        <li class="widget-post-wrapper">
                                <div class="widget-post-thumb">
                              <?php 
                                  echo st_post_thumbnail($post->ID);
                              ?>
                              </div>
                            <div class="widget-post-content">
                                <h3 class="widget-post-title"><a <?php echo $title; ?> href="https://wordpress.stackexchange.com/questions/309505/<?php the_permalink(); ?>" title="<?php echo the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                                <span class="widget-post-meta"><?php echo get_the_date(); ?> - <span><?php comments_number(__('0 Comment','magazon'),__('1 Comment','magazon'),__('% Comments','magazon') )?></span></span>
                            </div>
                        </li>
                <?php } ?>
             </ul>
            <?php } wp_reset_query() ;

            echo $after_widget;
       }

   }

   register_widget( 'STRecentPosts' );

0

Leave a Comment