Support » Fixing WordPress » WP_Query not working as expected inside widget

  • 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!

  • The topic ‘WP_Query not working as expected inside widget’ is closed to new replies.