0

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?

<?php 
    $postnumcat = new wp_query (array(
    'post_status' => 'publish',
    'post_type' => 'post',
    'cat' => '-1',
    'posts_per_page' => '-1',
    ));
?>

<?php if($postnumcat->have_posts()) :
while($postnumcat->have_posts()) : $postnumcat->the_post(); ?>

    the_title();
    the_content();

<?php endwhile; endif; wp_reset_query(); ?>  

even i putted <?php wp_reset_postdata(); ?> after endwhile like so:

<?php endwhile; wp_reset_postdata(); endif; wp_reset_query(); ?>   

and even like so:

<?php endwhile; wp_reset_postdata(); endif; ?> 

but it display nothing too :|

  • Is this your actual code? You don't have any "loop elements" to output – rudtek Dec 12 '18 at 18:05
  • 2
    Do yo uneed this custom loop? WordPress has archives for every post type by default – kero Dec 12 '18 at 18:11
  • @rudtek yes it is actual but instead of "loop element" you can consider the_title(). there is not any issue with that part – sh.dehnavi Dec 13 '18 at 6:27
  • @kero even for post? how can i display the posts of default post type (post)? – sh.dehnavi Dec 13 '18 at 6:28
3

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:

enter image description here

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

  • Hi my friend, thanks for helping. in my theme, i have index.php that does not include the latest posts. i wanted to display all of post in one page. but you say i should create a page template without using WP_Query and just use a loop. then go into settings, and under reading. am i right? so by doing so, what is the slug of my post archive? consider that, when we add a new post type with a slug (e.g: book), by default, all the posts of that custom post type will be displayed in yoursite.com/book but there is not true for posts for post type of post – sh.dehnavi Dec 13 '18 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.