How to query all posts in WordPress using the query_posts codex.

This article was written by in September 2, 2013, & may not be posted on other sites!
Original source url for this article: How to query all posts in WordPress using the query_posts codex.

How to query all posts in WordPress using the query_posts codex?
If you want to make an additional page template to query ALL posts in WordPress and display them on a page other than index. 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. This is how to:
<?php query_posts('showposts=-1'); ?>
The full code for a page template is available on the bottom of this article.

 

Still having trouble to query posts in WordPress?

If you are trying to create a more complex loop with different arguments, other than just query all posts wordpress. You should create a new fresh wp_query to query the posts instead. This is to avoid conflicts with the main query. something like this will work in page.php:

<?php wp_reset_query(); ?>
<?php
global $wp_query;
$new_query = null;
?>

<?php if(is_page(1)){
$args = array(
'posts_per_page' => -1,
'cat' => '8'
);
$new_query = new WP_Query($args);
}elseif(is_page(2)){
$args = array(
'posts_per_page' => -1,
'tag' => 'randomtag'
);
$new_query = new WP_Query($args);
}else{
$args = array(
'posts_per_page' => -1
);
$new_query = new WP_Query($args);
}
?>
<?php while ($new_query->have_posts()) : $new_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endwhile; ?>

Conflicts when trying to query all posts wordpress?

If your loop is conflicting with the WordPress main query you could try to reset the main query by writing this code <?php wp_reset_query(); ?> right before and after your own loop. You can also try to use get_posts( ); instead of query_posts( ); NOTE! – You need to write the category ID instead of category name with get_posts, something like this get_posts(‘cat=8’);
You can see the id numer on the bottom of your screen when hovering over the category name in the WordPress Admin > Posts > Categories.

How to query all posts in WordPress using the query_posts codex.

Below the full WordPress code for a basic page template to query all posts.

Just copy and paste the code below into a new page template and save it in your themes folder > page-templates. Save it as your-template.php for example. Create a new page in the Worpdress admin and choose the new template in the template dropdown to the right. The template will query all posts and will work on any random page in WordPress.

<?php
/**
* Template Name: Your template
* Author Olivia Hoback
* www.olivia.nu
*/



get_header(); ?>

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

<?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; ?>

</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 *