Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following loop, and only this loop, in my home.php file (apologies if I didn't paste the code properly). I have populated lorem ipsum posts, and noticed that of the five posts only four show up on the page. Any insights would be greatly appreciated.

<div itemscope itemtype="http://schema.org/Blog" class="left-column-container">

     <?php 
    // the query
    $the_query = new WP_Query( 'category_name=news'); ?>

    <!-- the loop -->
    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div itemprop="blogPost" id="post-<?php the_ID(); ?>" <?php post_class('snippet') ?>>

         <a itemprop="url" href="<?php the_permalink(); ?>" class="permalink"><p itemprop="headline" class="title"><?php the_title(); ?></p></a>

         <div class="snippet-meta">
              <p itemprop="datePublished" class="excerpt-meta date"><?php the_time('j M Y') ?></p> <p itemprop="keywords" class="excerpt-meta tags"><?php the_tags( '', ', ', '' ); ?> </p>
         </div><!-- End .snippet-meta -->

         <div class="excerpt">
             <?php the_excerpt(); ?>
         </div><!-- End .excerpt -->
     </div><!-- End .snippet -->

     <?php endwhile; ?>
     <!-- End list of news posts -->
</div><!-- End .left-column-container -->

<div class="cf" id="excerpt-nav-wrapper">
    <?php posts_nav_link( ' ', '<img src="' . get_bloginfo('stylesheet_directory') . '/images/prevarrow.png" class="nav-btns" id="prev" />', '<img src="' . get_bloginfo('stylesheet_directory') . '/images/nextarrow.png" class="nav-btns" id="next" />' ); ?>
</div><!-- End posts nav wrapper -->

    <?php else : ?>

    <div class="left-column-container">

        <h3>There are currently no news updates.</h3>
    </div>

    <?php endif; ?>
    <?php wp_reset_query(); ?>

</div>
share|improve this question
add comment

2 Answers 2

I found a solution to the problem I was having with WP_Query. What I was missing was the offset (which I did not realize needed to be set when dealing with custom queries).

First: Get the current page

// If the query var is set use it; otherwise, initialize it to one.
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

Second: Write the query

// First, initialize how many posts to render per page
$display_count = 2;

// Next, get the current page
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

// After that, calculate the offset
$offset = ( $page - 1 ) * $display_count;

// Finally, we'll set the query arguments and instantiate WP_Query
$query_args = array(
  'post_type'  =>  'post',
  'orderby'    =>  'date',
  'order'      =>  'desc',
  'number'     =>  $display_count,
  'page'       =>  $page,
  'offset'     =>  $offset
);
$custom_query = new WP_Query ( $query_args );

/*
 * Use your query here. Remember that if you make a call to $custom->the_post()
 * you'll need to reset the post data after the loop by calling wp_reset_postdata().
 */

Source: Tom McFarlin

share|improve this answer
add comment

Try to rewrite the query and dump it like:

<?php
$the_query = new WP_Query( 'category_name=news&posts_per_page=10');
var_dump($the_query);
?>

If the result is an empty array, it seems it can't query any post from news, then try to remove the category_name parameter to see what will show.

If the result is an array containing less posts and is not consist with the post count in your admin panel, The problem might be caused by cache plugin. Even if you've deactivated it, it's better to clear all cache files or use another browser to check. And there also might be functions which hook to the pre_get_posts action in your theme or plugin, to cause this.

If the result is an array with right post count, the problem should be caused by CSS or JS, for they might hide or remove your post so you can't see it.

share|improve this answer
1  
I found a blog post that actually fixed the problem I was having with WP_Query. What I was missing was the offset (which I didn't realize needed to be set when dealing with custom queries). Posting the link here, since it answers my question. tommcfarlin.com/wp_query-offset –  Codette Feb 2 at 22:20
    
@Codette Please don't post an answer as comment. Add a separate answer and then just explain step by step how to solve your problem (no plain link). –  kaiser Feb 5 at 15:38
    
@kaiser I apologize. I will do so now. –  Codette Feb 22 at 22:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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