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 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.

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();
        }
    }
?>
share|improve this question
    
Yes, you missed publishing the relevant code here :) –  brasofilo Jun 13 '12 at 17:23
    
<?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(); } } ?> –  Mdunbavan Jun 13 '12 at 17:27
    
Mark, sorry... first welcome to WPSE. After posting a question, you can edit it, adding new info to interact with some answer or comment. Your code belongs to the question, where it can be formatted for better visualization. –  brasofilo Jun 13 '12 at 17:39
add comment

2 Answers 2

up vote 1 down vote accepted

use wp_reset_query after your loop to restore the global post data for the main loop.

share|improve this answer
    
I had an idea that it would be a reset but I thought it was reset_query forgot about the wp in front as this has to find it in the db right? –  Mdunbavan Jun 13 '12 at 18:11
add comment

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);
    }
}
share|improve this answer
    
ah right. Cheers Johannes. That is extremely useful information on the side of looking at wp_queries. Starting to get my head around it now. –  Mdunbavan Jun 14 '12 at 19:55
    
@user1341368 If you're in the process of wrapping your head around queries in general, you should read this extremely informative answer by Rarst –  Johannes Pille Jun 14 '12 at 20:49
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.