1

I'm trying to display posts from regular and custom post types :

 <ul class="scroll">
   <?php 
    // the query
    $the_query = new WP_Query( 'cat=-4,-142,-143,-144' ); ?>

    <?php if ( $the_query->have_posts() ) : ?>

    <!-- pagination here -->

    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li><a href="<?php the_permalink() ?>">
            <div class="title-block">
                <div class="img-holder"><?php the_post_thumbnail('homepoststhumbnail'); ?><div class="title"><?php the_title(); ?></div></div>

            </div>
            <div class="text"> <?php the_excerpt(__('(more…)')); ?><?php the_field('questions'); ?></div>
            </a>
        </li>
        <?php endwhile; ?>
        <!-- end of the loop -->

        <!-- pagination here -->

        <?php wp_reset_postdata(); ?>

        <?php else:  ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; ?>
    </ul>

1 Answer 1

0

You're setting arguments for the main query in your pre_get_posts action, and then overwriting that query in the template by calling query_posts (which you should never use).

If you want to exclude categories, do that in the same pre_get_posts action:

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

function add_my_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() ){
        $query->set( 'post_type', array( 'post', 'miss_behave', 'emily_davies','gemma_patel','poppy_smythe' ) );
        $query->set( 'category__not_in', array(4, 142, 143, 144) );
    }
}

EDIT- using the arguments in an additional query:

$the_query = new WP_Query(
    array(
        'post_type' => array( 'post', 'miss_behave', 'emily_davies','gemma_patel','poppy_smythe' ),
        'category__not_in' => array(4, 142, 143, 144)
    )
);
14
  • Hm, I changed the in pre_get_posts code, and removed query-posts, but not the recent posts section is showing nothing...
    – Jaeeun Lee
    Apr 25, 2014 at 3:17
  • is the recent posts section the main query?
    – Milo
    Apr 25, 2014 at 3:42
  • Not sure what main query is, but I added this '$the_query = new WP_Query( $args );' to the template and now it works. Thanks.
    – Jaeeun Lee
    Apr 25, 2014 at 4:03
  • Ugh, it worked for a second, and now it doesn't...not sure what I did to make it work.
    – Jaeeun Lee
    Apr 25, 2014 at 4:05
  • The main query is the query that is run before the template is loaded, based on the requested URL. Its post or page data gets output via the normal loop within the template. If this is an additional query and not the main query, you should remove the pre_get_posts action, and move those arguments into a call to WP_Query.
    – Milo
    Apr 25, 2014 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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