WP_Query() returns null when results exist!

I really, really don’t get it.

On my archive.php and category.php it doesn’t find any content. I know the correct templates are loaded, but WP_Query just don’t find any content.

Read More

This is what i’ve got for archive.php:

    <?php

    if($displayMobileTheme){
        get_template_part('page', 'mobile');
        exit;
    }
    get_header();

    echo '<div class="blog-page">';
    get_sidebar('blog');
?>
    <h1>Arkiv: <?php echo ucfirst(get_the_date('F Y')); ?></h1>
    <?php
    $blog = new WP_Query(array(
        'post_type'     => 'post',
        'year'          => get_the_date('Y'),
        'monthnum'      => get_the_date('n')

    ));
    while($blog->have_posts()): $blog->the_post();
        get_template_part('excerpt');
    endwhile;
    wp_reset_query();
    ?>

</div> <!-- blog page -->
<?php
    get_footer();
?>

get_the_date() does return the correct year and monthnum, so why don’t i get any results?

Excerpt file, as requested:

    <?php if(!is_single(get_the_ID())) : ?>
    <article class="hentry excerpt clearfix" id="post-<?php the_ID(); ?>">
        <header class="left">
            <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <small class="entry-meta author">
                <?php $post_categories = wp_get_post_categories(get_the_ID()); ?>
                Skrivet av <?php the_author(); ?> den <?php the_time('j F Y'); ?>, 
                Kategorier: 
                <?php
                    $output = '';
                    foreach($post_categories as $c){
                        $cat = get_category($c);
                        $output .= '<a href="'.get_category_link($c).'">'.$cat->name.'</a>, ';
                    }
                    echo substr($output, 0, -2);
                ?>
            </small>
        </header>
        <div class="entry-content left">
            <?php if(has_post_thumbnail()): ?>
                <a class="left" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                    <div class="img-wrapper">
                        <?php the_post_thumbnail('blog_thumb', 'thumbnail'); ?>
                    </div>
                </a>
            <?php endif; ?>

            <?php the_excerpt();?>
            <a href="<?php the_permalink(); ?>">Läs mer</a>
            </p>
        </div>
    </article>
<?php endif; ?>

Related posts

Leave a Reply

1 comment

  1. The is_single function (used in your excerpt.php file) returns true only when the main query contains one single post. Since you’re calling get_template_part from an archives page, your query contains more than one post, so is_single returns false and your excerpt.php file bails. Remove the check for is_single and you should be good to go.

    More about is_single: http://codex.wordpress.org/Function_Reference/is_single

    More about conditional tags: http://codex.wordpress.org/Conditional_Tags

    Hope this helps.