There are several issues here, starting with the first part:
<?php
$postnumcat = new wp_query (array(
'post_status' => 'publish',
'post_type' => 'post',
'cat' => '-1',
'posts_per_page' => '-1',
));
Firstly, wp_query
is misspelt, it’s WP_Query
Some general notes too:
- Setting
posts_per_page
to-1
is bad for performance. Even if you don’t expect there to be many posts, set a super high number instead, but never say-1
as that means unlimited, even if your server can’t handle it. Use pagination if there are too many results - Setting
cat
to-1
tells WP to exclude the category with the term-1
, which can be extremely slow and expensive. Avoid these kinds of queries at all costs
Then for some reason, there’s a closing PHP tag, a space and newline, then an opening PHP tag, this will create unwanted whitespace:
?>
<?php if($postnumcat->have_posts()) :
Then we have the loop, which hasn’t been formatted correctly, but notice there is nothing inside the loop, all you’re going to get is a series of <!-- Loop elements -->
. The code needs to actually display things
while($postnumcat->have_posts()) : $postnumcat->the_post(); ?>
<!-- Loop elements -->
<?php endwhile; endif; wp_reset_query(); ?>
And finally, wp_reset_query
is for use with query_posts
, a function you should never use. Black list wp_reset_query
and never use it, there is no reason for you to use it.
Instead, lets go back to the original task:
i want to display archive of posts for post type of post. so i created a page template and insert the following codes but i display nothing. why? what should i do?
WordPress already does this! By default this is your homepage ( home.php
/archive.php
/index.php
). But if you wanted to put this archive on a different page, using a page template then throwing away the main query and using your own is a terrible way to do it.
Imagine you sent your assistant off to buy you a coffee every morning. Except after she came back from a 40 minute round trip, you told them “No, I want a Hot Chocolate”. Every morning you make them go out twice. Then the client complains your business is slow and doesn’t perform well.
On top of that, you’ve just lost a lot of the benefits of WP, e.g. pagination will break, and you’ll need to re-implement it to get it working again.
Instead, create a page called “Post Archive”, go into settings, and under reading, set it as your posts page:
Then, create a frontpage.php
and a home.php
in your theme so that you can style them both. Use a normal, standard, main post loop in both.
For reference
This is a standard main loop:
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_title();
the_content();
}
} else {
// no posts found
}
This is a custom post query:
$q = new WP_Query( [
// args go here
]);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
the_title();
the_content();
}
wp_reset_postdata();
} else {
// no posts found
}
And this is what you’re supposed to do if you want to modify which posts WordPress fetches, by telling it before it fetches them:
function my_home_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
// only show posts from category 123 on the homepage
$query->set( 'cat', '123' );
}
}
add_action( 'pre_get_posts', 'my_home_category' );
And finally, put each thing on its own line, and indent them. There are a lot of free editors that will automatically do this for you, e.g. Sublime Text, VS Code, Atom, etc