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.
surpress_filters
arg to bypass any filters that are jumping onto your query.