Why is querying posts messing up my pages?

I am working on a theme at the moment and I have set up a post template.

This post template is linked to some custom post types.

Read More

When I query_posts for my post type on the actual post template itself it makes the content disappear for some reason? Is there something I am missing here?

Thanks,
Mark

My loop is as follows:

<?php
    $query = 'posts_per_page=10&post_type=articles';
    $queryObject = new WP_Query($query);

    // The Loop...
    if ($queryObject->have_posts()) {
        while ($queryObject->have_posts()) {
            $queryObject->the_post(); the_title(); the_content();
        }
    }
?>

Related posts

Leave a Reply

2 comments

  1. For the sake of completeness, while using wp_reset_query() isn’t necessarily wrong, it is an unnecesary extra operation, if run after a secondary query with a new instance of the WP_Query class. The $wp_query object isn’t altered, so it needs not be reset. Only the $post global requires a reset. Hence, wp_reset_postdata() would be sufficient in this case.

    See /wp-includes/query.php for reference:

    /**
    * Destroy the previous query and set up a new query.
    *
    * This should be used after {@link query_posts()} and before another {@link
    * query_posts()}. This will remove obscure bugs that occur when the previous
    * wp_query object is not destroyed properly before another is set up.
    *
    * @since 2.3.0
    * @uses $wp_query
    */
    function wp_reset_query() {
        unset($GLOBALS['wp_query']);
        $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
        wp_reset_postdata();
    }
    
    /**
    * After looping through a separate query, this function restores
    * the $post global to the current post in the main query
    *
    * @since 3.0.0
    * @uses $wp_query
    */
    function wp_reset_postdata() {
        global $wp_query;
        if ( !empty($wp_query->post) ) {
            $GLOBALS['post'] = $wp_query->post;
            setup_postdata($wp_query->post);
        }
    }