Function query_posts not working in WordPress template or page.php, how to fix it!

This article was written by in September 9, 2013, & may not be posted on other sites!
Original source url for this article: Function query_posts not working in WordPress template or page.php, how to fix it!

query_posts not working?
Is the query_posts not working in your WordPress template or page.php? This is because we are in conflict with the main loop or missing the right arguments. This is how to fix it!

 

Query_posts within a page-template

If you want to make an additional page template to query all posts in WordPress and display them in a random page other than index, keep on reading. Just writing <?php query_posts( ); ?> in your Wordpress page template won’t work since the function is missing the right arguments. If this is the case you need to add the code “showposts=-1″ to make it work for random pages. Try this:
<?php query_posts("showposts=-1"); ?>

Code example query_posts and how to query alla posts:
<?php wp_reset_query(); ?>
<?php query_posts("showposts=-1"); ?>
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>

 

Query_posts from within the page.php or any other WordPress theme-file + tag or category conditions

Since the main loop can cause trouble when creating custom querys, you should try to create a new fresh query if you have a lot of arguments / conditions in your query_posts loop. If you use query_posts( ); in your page.php (code example above) this will show you all posts with no chance of adding category / tags arguments. To do this create a new fresh query instead, check out this code example below altering the page.php for the Twenty Twelve theme. (You should always work with a childtheme and not altering directly in Twenty Twelve theme)

Function query_posts not working in WordPress template or page.php, how to fix it!

 

Below full code for altering the page.php. If page = 1, query posts from category 1 etc.
Just copy and paste this code into a php-file and name it page.php. Put it in your childtheme folder. You need to create some pages and posts and match the IDs and categories in the code to get this file to work in WordPress.

<?php
/**
* The template for displaying all pages.
*
* Author Olivia Hoback
* www.olivia.nu
*
* @package WordPress
* @subpackage Twenty_Twelve
* @since Twenty Twelve 1.0
*/

get_header(); ?>

<div id="primary" class="site-content">
<div id="content" role="main">


<?php //this will still display the current page content using the main loop ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>



<?php //below the code for our custom query------------------ ?>
<?php wp_reset_query(); ?>
<?php
global $wp_query;
$new_query = null;
?>

<?php if(is_page(1)){
$args = array(
'posts_per_page' => -1,
'cat' => '1'
);
$new_query = new WP_Query($args);
}elseif(is_page(2)){
$args = array(
'posts_per_page' => -1,
'cat' => '2'
);
$new_query = new WP_Query($args);
}else{
$args = array(
'posts_per_page' => -1
);
$new_query = new WP_Query($args);
}
?>

<div class="container">

<?php /* Start the Loop */ ?>
<?php while ($new_query->have_posts()) : $new_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>

</div><!--end of .container-->





<?php // Reset Query
wp_reset_query(); ?>


</div><!-- #content -->
</div><!-- #primary -->


<?php get_footer(); ?>

Spread the word

Facebooktwittergoogle_pluslinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *