Sheixt
Sheixt

Reputation: 2626

Wordpress query_posts() not returning list of posts whilst excluding one category

I'm having some trouble witin one of my page templates for my Wordpress theme. I wish to list all post but exclude those from one category (id: 4).

Below I have used query_posts() as per the Codex documentation:

<?php get_header(); ?>

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

    <header
            <?php
            if ( has_post_thumbnail()) {
                $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
                echo 'style="background-image:url(' . $large_image_url[0] . ')"';
            }
            ?>
    >
            <div class="row">
                <div class="page-title small-12 large-8 columns">
                    <h1>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </h1>
                </div>
            </div>
    </header>

    <?php get_template_part( 'content', 'headerbanner' ); ?>

<?php endwhile;?>

    <section class="container main-content">
            <div class="row">
                <div id="content" class="small-12 columns">
                    <?php query_posts($query_string . '&cat=-4'); ?>
                    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

                        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

                        <?php the_content(); ?>

                    <?php endwhile; else: ?>

                        <p>Sorry, no posts matched your criteria.</p>

                    <?php endif; ?>
                </div>
            </div>
    </section>

<?php get_footer(); ?>

The problem is that this does not return a list of the posts. Instead it only returns the details for the page itself (in this case called "blog").

Upvotes: 0

Views: 575

Answers (2)

Sheixt
Sheixt

Reputation: 2626

Amending the code from inside #content to the following solved this problem:

<?php query_posts('cat=-4'); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    <?php get_template_part( 'content', get_post_format() ); ?>

<?php endwhile; ?>

Upvotes: 1

Bhumi Shah
Bhumi Shah

Reputation: 9476

here you need to pass post_type

query_posts($query_string . '&cat=-4&post_type=post');

Upvotes: 0

Related Questions