WordPress.org

Ready to get started?Download WordPress

Forums

WP_Query not working as expected inside widget (1 post)

  1. alex27
    Member
    Posted 3 years ago #

    Hi!

    I'm struggling to make this piece of code working:

    </p>
    <ul>
            <?php
                $temp_query = $wp_query;
                $params = array('category__in'=> array(12,13,8,14,4,16,18,19,20,21),'posts_per_page'=>5);
                $wp_query = new WP_Query($params);
                while (have_posts()) {
                    the_post();
    ?>
              <a><?php the_title(); ?></a>
      <?php
                } // end custom loop
            $wp_query = $temp_query;
            ?>

    This code is in one of my custom theme widgets, placed in the sidebar. The goal is to display 5 newest posts from listed categories. But instead on category page it shows 10 posts (the categories parameter works fine) and in single post it shows only one newest post.

    The weird part is, when I copied this code and put it as it is in single.php (before them main loop), it works perfectly.

    I'll appreciate any suggestions on how to make it work inside a widget!

Topic Closed

This topic has been closed to new replies.

About this Topic