• Hi!
    I have a problem with WP_Query loop. I just want to query 8 latest posts without pagination and any other filters, but wordpress returns to me ALL posts, not only 8.

    My WP query
    $loop2 = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 8, 'post_status'=>'publish'));

    But if I`d try to add a category filter and my query works nice

    $loop2 = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 8, 'post_status'=>'publish','cat'=>154));

    but I won`t have any filters and I just want to display 8 posts from ALL categories.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Like this:
    $loop2 = new WP_Query( 'posts_per_page' => 8 );
    or
    $loop2 = new WP_Query( 'posts_per_page=8' );
    WordPress Codex : Class Reference/WP Query

    Thread Starter polushinmk

    (@polushinmk)

    NeoTechnomad, I tried this code and it display all posts, not only 8 but ALL. Why? this is a bug? or I need some other config options?

    Try:
    $loop2_query = new WP_Query( 'posts_per_page' => 8 );
    or
    $loop2_query = new WP_Query( 'posts_per_page=8' );
    Pagination Parameters

    This is directly from my dev site where I use multiple WP_query…

    <?php $the_query = new WP_Query( 'posts_per_page=3' ); ?>
    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    	<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    		<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    		<?php if ( has_post_thumbnail() ) {
    			the_post_thumbnail();
    		} ?>
    		<?php echo excerpt(30); ?>
    	</div>
    
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    <?php endif; ?>
    Thread Starter polushinmk

    (@polushinmk)

    example of my loop

    <?php
    $the_query = new WP_Query('posts_per_page=8');?>
    <ul class="latestpost">
    <?php if ($the_query->have_posts()): ?>
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
    <li>                       						   <?php if(has_post_thumbnail()) { ?>
    <?php
    $thumb = get_post_thumbnail_id();
    $img_url = wp_get_attachment_url( $thumb,'full'); //get img URL
    $image = aq_resize( $img_url, 211, 158, true ); //resize & crop img
    ?>
    <figure class="featured-thumbnail-rel">
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>
        <img src="<?php echo $image ?>" alt="<?php the_title(); ?>" /></a>
    </figure>
    <?php } ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(''); ?></a></h2>
    <div class="excerpt"><?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,26);?></div>
    <div class="link"><a href="<?php the_permalink(); ?>">Читать далее</a></div>
    </li>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    <?php endif; ?>
    </ul>

    and this loop doesn`t work as I expected ?? I think this happens cause I use multiple loop on my front page.
    But why when I add a ‘cat’ parameter loop works nice?

    There were several minor errors in the code. This should work…

    <?php $the_query = new WP_Query( 'posts_per_page=8' ); ?>
    <ul class="latestpost">
    	<?php if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post(); ?>
    	<li>
    		<?php
    			$thumb = get_post_thumbnail_id();
    			$img_url = wp_get_attachment_url( $thumb,'full'); //get img URL
    			$image = aq_resize( $img_url, 211, 158, true ); //resize & crop img
    		?>
    		<figure class="featured-thumbnail-rel">
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>
    			<img src="<?php echo $image ?>" alt="<?php the_title(); ?>" />
    			</a>
    		</figure>
    
    		<h2>
    			<a href="<?php the_permalink(); ?>">
    				<?php the_title(); ?>
    			</a>
    		</h2>
    
    		<div class="excerpt">
    			<?php $excerpt = get_the_excerpt(); echo my_string_limit_words( $excerpt, 26 ); ?>
    		</div>
    		<div class="link">
    			<a href="<?php the_permalink(); ?>">
    				<?php the_title(); ?>
    			</a>
    		</div>
    	</li>
    	<?php
    	endwhile;
    	wp_reset_query();
    	endif;
    	?>
    </ul>
    Thread Starter polushinmk

    (@polushinmk)

    I create special page for your example of loop and have the same behaviour ??

    https://express-kirov.ru/test/

    This page displays all posts, ignoring the ‘posts_per_page=8’ option

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Trouble with WP_Query’ is closed to new replies.