0

I've found almost the same question: https://stackoverflow.com/questions/4796165/multiple-loops-in-wordpress-second-loop-not-resetting but the answer doesn't work for me.

I have "portfolio" post type, and I'm working on widget displaying all portfolio items.

This code works perfectly on page-portfolio:

$loop = new WP_Query( array( 
        'post_type' => 'portfolio',   
        'posts_per_page' => 5 ) );

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>         
 (...)
<?endwhile?>

But when I implement this in my widgets - the "post_type" field doesn't seem to work. No matter what I type as post type - it always displays POSTS instead of PORTFOLIO ITEMS. Even if the post type doesn't even exist (and as we all know it should display nothing if there's no such post type!).

I believe the problem is aside from my widget there's blog section (loop from loop.php, no code to show, because there's just only "while ( have_posts() ) : the_post();")

[edit]

I may be wrong, deleted every line of code from loop.php and my widget still shows posts.

Any ideas how to fix that?

[edit #2]

Ok, so my site looks just like every normal Wordpress blog:

header.php
loop.php > widget area
footer.php

There is almost no PHP code in header and footer (at least no loops etc.).

The loop.php is also I'd say normal:

 <?php if ( ! have_posts() ) : ?> 
    (...)
    <?php endif; ?> 

    <?php while ( have_posts() ) : the_post(); ?> 
    (...)
    <?php endwhile; ?>  

In fact the widget is ALSO not very unique:

function myWidgetName_load_widgets() {
    register_widget( 'myWidgetName' );
}

class myWidgetName extends WP_Widget {

    function myWidgetName() {
(...)
}

function widget( $args, $instance ) {
        extract( $args );
        $title = apply_filters('widget_title', $instance['title'] );
        $number = $instance['number'];

        echo $before_widget;
        if ( $title )
            echo $before_title . $title . $after_title;
        if ( $number )
    echo "<ul>"; ?>          
        <?php 
     $loopWithSuperUniqueName = new WP_Query( array( 
        'post_type' => 'anything', /* and this doesn't work */   
        'posts_per_page' => 15 ) );
    ?>

     <?php while ( $loopWithSuperUniqueName->have_posts() ) : $loopWithSuperUniqueName->the_post(); ?>  
     <p><?php the_title(); ?></p> /* it always displays POSTS even if 'post_type' => 'alalaaolbadiobladaWhathever' */
     <?php endwhile; ?>  




    <?php echo "</ul>"; ?> 
        <?php
        echo $after_widget;
    }

function update( $new_instance, $old_instance ) {
(...)
}

 function form( $instance ) {  
(...)
}

etc.

The widget's code is OK, because I've developed a few similar widgets and they WORK.

I'm starting to think there's something wrong with Wordpress, not my code.

6
  • @Wordpressor: Maybe there is a plugin that interferes with your query? Try disabling all plugins and report back whether that changed anything.
    – Jan Fabry
    Feb 22, 2011 at 14:35
  • 1
    Might try passing in the surpress_filters arg to bypass any filters that are jumping onto your query.
    – t31os
    Feb 22, 2011 at 14:38
  • @Jan Fabry it displays items of wrong post type even after disabling all other widgets, so that's not the point :( @t31os I'll try, but I'm not sure how to :) Feb 22, 2011 at 14:52
  • 1
    @Wordpressor: After disabling all widgets or all plugins?
    – Jan Fabry
    Feb 22, 2011 at 14:57
  • @Jan Fabry, right, my mistake. I've disabled all plugins this time, nothing changes. Feb 22, 2011 at 15:06

2 Answers 2

1

It sounds like your widget references the global $loop for some reason. This could possibly be due to the internals of the Widget class or due to interference of other plugins, i guess.

As a first step, to be on the safe side, try using a different variable name. The global loop is referred to as $loop, by simply renaming yours to $myloop you can avoid any confusions between global and local.

Secondly, if the trouble persists, test your widget on a 'page' type page. Or any other 'single' page. Does it only show a single row now?

8
  • Changing loop's name was the first thing that came to my mind, but it didn't help. I've been trying even with query_posts instead of WP_Query. For page/single/anything else it shows posts. All the time. But posts_per_page work and if I type 1 there then I'm getting 1 post, 2 - 2 posts etc. Everything just seem perfectly fine. Excepting this bug with post types. The funny thing is the same loop just work amazingly well but not in widget. And the guy from the first post had the same problem, but his answer doesn't work in my case as well. Global $post also didn't change a thing. Feb 22, 2011 at 15:08
  • 1
    I'm afraid you'll have to post the full code for at least that particular method. So you tried using get_posts()? Also, are you setting any query related or parse_query related filters or actions? Are you doing a global $loop, global $wp_query, global $posts or anything the likes anywhere?
    – wyrfel
    Feb 22, 2011 at 15:27
  • I'm using the same code as above with changed variable names. I'm doing global xxx nowhere. Feb 22, 2011 at 16:04
  • 1
    What's within the loop?
    – wyrfel
    Feb 22, 2011 at 16:08
  • @wyrfel, check the first post if you still have faith Feb 22, 2011 at 20:04
-2

i could solve this problem using wp_reset_query();

at the beginning of the widget

1
  • Can you explain why you think this could work?
    – s_ha_dum
    Jan 5, 2014 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.