admin管理员组文章数量:1420086
I want to show all posts like mywebsite/postname/1/
, mywebsite/postname/2/
...... mywebsite/postname/7/
..
Below code works perfectly for me, the only problem is that it shows only first posts mywebsite/postname/
...
How can I do this?
<?php
/**
* Template Name: Random Post
* This template will only display the content you entered in the page editor
*/
?>
<html>
<head>
</head>
<body>
<?php
/*
Random Post Picker
Use on page to send viewer to random post optionally mod query
*/
// set arguments for WP_Query on published posts to get 1 at random
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'rand',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'after' => '2 week ago'
)
)
);
// It's time! Go someplace random
$my_random_post = new WP_Query ( $args );
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
// redirect to the random post
wp_redirect ( get_permalink () );
exit;
}
?>
</body>
</html>
I want to show all posts like mywebsite/postname/1/
, mywebsite/postname/2/
...... mywebsite/postname/7/
..
Below code works perfectly for me, the only problem is that it shows only first posts mywebsite/postname/
...
How can I do this?
<?php
/**
* Template Name: Random Post
* This template will only display the content you entered in the page editor
*/
?>
<html>
<head>
</head>
<body>
<?php
/*
Random Post Picker
Use on page to send viewer to random post optionally mod query
*/
// set arguments for WP_Query on published posts to get 1 at random
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'rand',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'after' => '2 week ago'
)
)
);
// It's time! Go someplace random
$my_random_post = new WP_Query ( $args );
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
// redirect to the random post
wp_redirect ( get_permalink () );
exit;
}
?>
</body>
</html>
Share
Improve this question
edited Aug 23, 2017 at 14:03
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Aug 23, 2017 at 12:16
adi kicaadi kica
111 gold badge1 silver badge3 bronze badges
3
|
3 Answers
Reset to default 6Doing this in PHP is an awful idea:
- This page is impossible to cache
- ordering by random is extremely expensive to query, involving creating temporary database tables, and scans, as it has to copy the entire posts table, then randomly re-order the posts, then finally do the actual query on the new table before destroying it
- This opens you up to resource exhaustion attacks
For example, this command will ping your random post URL repeatedly. Ran enough times on enough computers, it will bring down your database:
for i in `seq 1 20000`; do curl http://mywebsite/postname; done
If you're on a cheap shared host, it may be enough to call your URL in several browser tabs at the same time to trigger problems.
Not to mention that your redirect sends out HTTP headers, but the code outputs tags beforehand, so headers have already been sent, breaking things and triggering PHP warnings.
The Super Fast Easy Alternative
So instead, do a normal query ordered by data, and output the data as JSON. Then in javascript on the browser, randomly pick one of those posts and do a client side redirect.
This way the page can be cached, your database is protected, and the browser does all the heavy lifting. The database query will be very fast in comparison
Now your problem is as simple as outputting a bit of data in a list, randomly picking something from the list in JS, then using window.location= ...
to redirect. No WP knowledge necessary
Only 1 post is displaying on your side because of you are using exit()
in your loop. In your code when WP_Query start and loop exit after one counter.
Remove exit()
and use this code:
<?php
/*
* Random Post Picker
* Use on page to send viewer to random post optionally mod query
*/
// set arguments for WP_Query on published posts to get 1 at random
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'rand',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'after' => '2 week ago'
)
)
);
// It's time! Go someplace random
$my_random_post = new WP_Query ( $args );
if($my_random_post->have_posts()){
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
echo '<a href="'.get_the_permalink().'">'.get_the_title().'</a>';
}
}
?>
You don't need to redirect the user to the random post page, and you can't even if you want, because you can't be redirected to multiple places at the same time.
So, simply output your random posts inside your loop:
while ( $my_random_post->have_posts () ) {
$my_random_post->the_post ();
echo '<a href="'.get_the_permalink().'">'.get_the_title().'</a>';
}
本文标签: wp querywpquery random post
版权声明:本文标题:wp query - wp_query random post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745322506a2653441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
rand
is extremely slow and expensive, and that page can't be cached. Any vaguely competent hacker can now launch a cheap denial of service attack by pinging that URL repeatedly, to bring down your database server via resource exhaustion – Tom J Nowell ♦ Commented Aug 23, 2017 at 13:01