WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is my category / archive page

i want to include a template before loop

but i cant get posts based on category, it showing latest post only on every page.

when i removed this line before the loop

<?php include( TEMPLATEPATH . '/includes/tb/hot_posts.tpl.php' ); ?>

it work smoothly, but i need this template before the loop

this is code (archive / category )

<div class="Hot_post">

<?php include( TEMPLATEPATH . '/includes/tb/hot_posts.tpl.php' ); ?>

<?php if (have_posts()) : $count = 0; ?>

and this is code from template (includes/tb/hot_posts.tpl.php)

 <?php query_posts('meta_key=post_views_count&order=DESC'); ?> 
 <?php if ( have_posts() ) : while (   have_posts() ) : the_post(); ?> 

please guide me

share|improve this question
up vote 1 down vote accepted

try and add

<?php wp_reset_query(); ?> 

at the end of your template. http://codex.wordpress.org/Function_Reference/wp_reset_query

share|improve this answer

Your problem has nothing at all to do with including a template-part file (for which, by the way, you should be using get_template_part() or locate_template()), and everything to do with Doing It Wrong with respect to query_posts().

If you're trying to override the main Loop query, then you need to make sure you're doing so correctly. If you need to maintain the default query context, and append query arguments, then you have to maintain the default $wp_query. The easiest way to do so is as described in the Codex:

global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'product' ) );
query_posts( $args );

To use your custom arguments, for example:

// Globalize $wp_query
global $wp_query;
// Define custom query args
$custom_query_args = array(
    'meta_key' => 'post_views_count',
    'order' => 'DESC'
// Merge custom query args with default query
$custom_query = array_merge( $wp_query->query, $custom_query_args );
// Query posts based on modified query arguments
query_posts( $custom_query );

If, by chance, you're intending to perform a secondary loop, then you don't want to use query_posts() at all, but instead need to use WP_Query() or get_posts().

share|improve this answer

Your Answer


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.