admin管理员组文章数量:1130349
I have this custom query:
if (isset($_COOKIE['myCookie']) && $_COOKIE['myCookie'] == $f_r) {
$args = array(
'posts_per_page' => 4,
'nopaging' => false,
'order' => 'ASC',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'Count',
'field' => 'slug',
'terms' => $f_r,
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
<a href="<?php the_field('the_link'); ?> ">
<img src="<?php echo the_field('the_img'); ?>" />
</a>
} else {
echo 'Oops! Something went wrong!';
}
wp_reset_postdata();
The query works, but it does not reset the data. Every time a visitor lands on the page where the query runs, it shows the exact same 4 posts as it always does.
This code run just fine in my local environment (fetching different posts each time it runs), but on the live server it seems to be "fixed" to 4 specific posts.
I don't understand why this is happening, so I could really use some advice here. Thanks!
Ps. It's not the plugins or theme, because it works fine in a local environment.
I have this custom query:
if (isset($_COOKIE['myCookie']) && $_COOKIE['myCookie'] == $f_r) {
$args = array(
'posts_per_page' => 4,
'nopaging' => false,
'order' => 'ASC',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'Count',
'field' => 'slug',
'terms' => $f_r,
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
<a href="<?php the_field('the_link'); ?> ">
<img src="<?php echo the_field('the_img'); ?>" />
</a>
} else {
echo 'Oops! Something went wrong!';
}
wp_reset_postdata();
The query works, but it does not reset the data. Every time a visitor lands on the page where the query runs, it shows the exact same 4 posts as it always does.
This code run just fine in my local environment (fetching different posts each time it runs), but on the live server it seems to be "fixed" to 4 specific posts.
I don't understand why this is happening, so I could really use some advice here. Thanks!
Ps. It's not the plugins or theme, because it works fine in a local environment.
Share Improve this question asked Jan 9, 2019 at 15:39 Steve RodgersSteve Rodgers 156 bronze badges 4- Who is your host? some managed WP hosts (ie. WPEngine) disable rand by default – mrben522 Commented Jan 9, 2019 at 15:41
- I see, and my host is ofc WPEngine... Do you know a workaround? Or is there a better way to get rand? @mrben522 – Steve Rodgers Commented Jan 9, 2019 at 15:45
- Do you (or your hosting company) use any cache on that site? – Krzysiek Dróżdż Commented Jan 9, 2019 at 16:14
- contact WPEngine support via their live chat. They'll turn it back on if you ask nicely – mrben522 Commented Jan 9, 2019 at 19:02
3 Answers
Reset to default 1If order by RAND is disabled at server level, you can try doing the rand by hand by running the query and get the ids first, then running a new query from the result including x random post ids:
$get_all_args = array(
'fields' => 'ids',
'posts_per_page' => 99,
'order' => 'DESC',
'orderby' => 'modified',
'tax_query' => array(
array(
'taxonomy' => 'Count',
'field' => 'slug',
'terms' => $f_r,
)
)
);
$query_all = new WP_Query( $get_all_args );
// additional code and checks
// ...
// ...
wp_reset_postdata();
$args = array(
'post__in ' => array_rand(array_flip( $query_all ), 4)
);
$query = new WP_Query( $get_all_args );
// additional code and checks
// ...
// ...
wp_reset_postdata();
You will get 4 random posts from the latest modified posts, notice I'm not using 'posts_per_page'=> -1 since this is not a good practice and can harm your site speed
you have missing a } after while, if you check
while ( $query->have_posts() ) {
} else {
Please try with
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
<a href="<?php the_field('the_link'); ?> ">
<img src="<?php echo the_field('the_img'); ?>" />
</a>
}
} else {
echo 'Oops! Something went wrong!';
}
wp_reset_postdata();
wp_reset_query();
try to use
wp_reset_query();
wp_reset_postdata();
As you can see here: documentation with wp_reset_query and wp_reset_postdata reset Query and Original Post Data
I have this custom query:
if (isset($_COOKIE['myCookie']) && $_COOKIE['myCookie'] == $f_r) {
$args = array(
'posts_per_page' => 4,
'nopaging' => false,
'order' => 'ASC',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'Count',
'field' => 'slug',
'terms' => $f_r,
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
<a href="<?php the_field('the_link'); ?> ">
<img src="<?php echo the_field('the_img'); ?>" />
</a>
} else {
echo 'Oops! Something went wrong!';
}
wp_reset_postdata();
The query works, but it does not reset the data. Every time a visitor lands on the page where the query runs, it shows the exact same 4 posts as it always does.
This code run just fine in my local environment (fetching different posts each time it runs), but on the live server it seems to be "fixed" to 4 specific posts.
I don't understand why this is happening, so I could really use some advice here. Thanks!
Ps. It's not the plugins or theme, because it works fine in a local environment.
I have this custom query:
if (isset($_COOKIE['myCookie']) && $_COOKIE['myCookie'] == $f_r) {
$args = array(
'posts_per_page' => 4,
'nopaging' => false,
'order' => 'ASC',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'Count',
'field' => 'slug',
'terms' => $f_r,
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
<a href="<?php the_field('the_link'); ?> ">
<img src="<?php echo the_field('the_img'); ?>" />
</a>
} else {
echo 'Oops! Something went wrong!';
}
wp_reset_postdata();
The query works, but it does not reset the data. Every time a visitor lands on the page where the query runs, it shows the exact same 4 posts as it always does.
This code run just fine in my local environment (fetching different posts each time it runs), but on the live server it seems to be "fixed" to 4 specific posts.
I don't understand why this is happening, so I could really use some advice here. Thanks!
Ps. It's not the plugins or theme, because it works fine in a local environment.
Share Improve this question asked Jan 9, 2019 at 15:39 Steve RodgersSteve Rodgers 156 bronze badges 4- Who is your host? some managed WP hosts (ie. WPEngine) disable rand by default – mrben522 Commented Jan 9, 2019 at 15:41
- I see, and my host is ofc WPEngine... Do you know a workaround? Or is there a better way to get rand? @mrben522 – Steve Rodgers Commented Jan 9, 2019 at 15:45
- Do you (or your hosting company) use any cache on that site? – Krzysiek Dróżdż Commented Jan 9, 2019 at 16:14
- contact WPEngine support via their live chat. They'll turn it back on if you ask nicely – mrben522 Commented Jan 9, 2019 at 19:02
3 Answers
Reset to default 1If order by RAND is disabled at server level, you can try doing the rand by hand by running the query and get the ids first, then running a new query from the result including x random post ids:
$get_all_args = array(
'fields' => 'ids',
'posts_per_page' => 99,
'order' => 'DESC',
'orderby' => 'modified',
'tax_query' => array(
array(
'taxonomy' => 'Count',
'field' => 'slug',
'terms' => $f_r,
)
)
);
$query_all = new WP_Query( $get_all_args );
// additional code and checks
// ...
// ...
wp_reset_postdata();
$args = array(
'post__in ' => array_rand(array_flip( $query_all ), 4)
);
$query = new WP_Query( $get_all_args );
// additional code and checks
// ...
// ...
wp_reset_postdata();
You will get 4 random posts from the latest modified posts, notice I'm not using 'posts_per_page'=> -1 since this is not a good practice and can harm your site speed
you have missing a } after while, if you check
while ( $query->have_posts() ) {
} else {
Please try with
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
<a href="<?php the_field('the_link'); ?> ">
<img src="<?php echo the_field('the_img'); ?>" />
</a>
}
} else {
echo 'Oops! Something went wrong!';
}
wp_reset_postdata();
wp_reset_query();
try to use
wp_reset_query();
wp_reset_postdata();
As you can see here: documentation with wp_reset_query and wp_reset_postdata reset Query and Original Post Data
本文标签: custom post typesWPQuery not resetting after wpresetpostdata
版权声明:本文标题:custom post types - WP_Query not resetting after wp_reset_postdata 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749026176a2305140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论