$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - why does not my wp_query work?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - why does not my wp_query work?

matteradmin11PV0评论

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 :|

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 :|

Share Improve this question edited Dec 13, 2018 at 6:37 sh.dehnavi asked Dec 12, 2018 at 17:38 sh.dehnavish.dehnavi 411 silver badge12 bronze badges 4
  • Is this your actual code? You don't have any "loop elements" to output – rudtek Commented Dec 12, 2018 at 18:05
  • 2 Do yo uneed this custom loop? WordPress has archives for every post type by default – kero Commented Dec 12, 2018 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 Commented Dec 13, 2018 at 6:27
  • @kero even for post? how can i display the posts of default post type (post)? – sh.dehnavi Commented Dec 13, 2018 at 6:28
Add a comment  | 

1 Answer 1

Reset to default 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:

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far