Search code examples
wordpresscustom-wordpress-pages

Custom wordpress latest posts plugin will not limit posts


Have been fiddling around with the while loop as well as the query params but can't seem to figure out what is causing the following custom code to not limit posts:

/**
 * Plugin Name: Latest posts
 */

add_action( 'widgets_init', 'weart_latest_posts' );

function weart_latest_posts() {
    register_widget( 'weart_latest_posts' );
}

class weart_latest_posts extends WP_Widget {

    //widget setup
    function weart_latest_posts() {
        $widget_ops = array( 'classname' => 'weart_latest_posts', 'description' => esc_html__('A widget that displays the latest posts.', 'custom') );
        $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'weart_latest_posts' );
        $this->__construct( 'weart_latest_posts', esc_html__('Custom: Latest Posts Widget', 'custom'), $widget_ops, $control_ops );
    }

    //display the widget
    function widget( $args, $instance ) {
        extract( $args );
        global $post;
        $title = apply_filters('widget_title', $instance['title'] );
        $number = $instance['number'];

        /* Before widget (defined by themes). */
        echo $before_widget;

        /* Display the widget title if one was input (before and after defined by themes). */
        if ( $title )
            echo $before_title . $title . $after_title;
        ?>

    <?php 
        $args1 = array(
        'cat' => 'RECIPES',
        'nopaging' => false,
        'posts_per_page' => '5',
        'found_posts' => '5',
        'max_num_pages' => '5',
        'post_count' => '5',
        'order'  => 'DESC',
        'orderby' => 'ID',
    );



         $query = new WP_Query( $args1 ); 
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();

            ;?>


<a href="<?php the_permalink(); ?>" class="block cf item border-top">
              <?php if ( has_post_thumbnail()) : ?>
              <div class="featured-image col col-one-third img" data-mh="latest_posts_height">
                <div class=""><div class="bg lazy" data-src="<?php the_post_thumbnail_url( 'weart-related-thumb'); ?>"></div></div>
              </div>
                <?php endif; ?>
              <div class="text col <?php if ( has_post_thumbnail()) : ?>col-two-third<?php endif; ?>" data-mh="latest_posts_height">
                <div class="<?php if ( has_post_thumbnail()) : ?>inner-left text-vertical<?php endif; ?>">
                    <h3 class="title h4"><?php the_title(); ?></h3>
                    <div class="meta c-grey link-grey meta-meta"><time datetime="<?php echo esc_attr(get_the_date('Y-m-d')); ?>"> <?php echo esc_attr(get_the_date()); ?></time></div>
                </div>
              </div>
            </a>


<?php }

} else {

} wp_reset_postdata(); 

?>





        <?php

        /* After widget (defined by themes). */
        echo $after_widget;

    }

    //update widget
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['number'] = '5';

        return $instance;
    }

    //form for update
    function form( $instance ) {
        //defaults
        $defaults = array( 'title' => esc_html__('Latest Recipes', 'custom'), 'number' => 5, 'popular_days' => 30 );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

        <p><!-- title -->
            <label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>">Title:</label>
            <input id="<?php echo esc_attr($this->get_field_id( 'title' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'title' )); ?>" value="<?php echo esc_attr($instance['title']); ?>" style="width:90%;" />
        </p><!-- title -->

        <p><!-- posts num -->
            <label for="<?php echo esc_attr($this->get_field_id( 'number' )); ?>">Number of posts to display:</label>
            <input id="<?php echo esc_attr($this->get_field_id( 'number' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'number' )); ?>" value="<?php echo esc_attr($instance['number']); ?>" size="3" />
        </p><!-- posts num -->


    <?php
    }
}

?> 

I thought the query params would lead to what I needed and played around with having them both as a variable and also directly in the statement.


Solution

  • Try to remove these:

    'nopaging' => false,
    'found_posts' => '5',
    'max_num_pages' => '5',
    'post_count' => '5',
    

    And pass a category ID to this:

    'cat' =>