admin管理员组文章数量:1130349
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 |1 Answer
Reset to default 3There 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_pageto-1is bad for performance. Even if you don't expect there to be many posts, set a super high number instead, but never say-1as that means unlimited, even if your server can't handle it. Use pagination if there are too many results - Setting
catto-1tells 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
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
1 Answer
Reset to default 3There 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_pageto-1is bad for performance. Even if you don't expect there to be many posts, set a super high number instead, but never say-1as that means unlimited, even if your server can't handle it. Use pagination if there are too many results - Setting
catto-1tells 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
本文标签: wp querywhy does not my wpquery work
版权声明:本文标题:wp query - why does not my wp_query work? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749103635a2316394.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


the_title(). there is not any issue with that part – sh.dehnavi Commented Dec 13, 2018 at 6:27