0

OK, i wanted my page to post a random post in every 24 hours , so i found that code over the internet,

I was trying to understand the code and added the comments to those lines which i understood, however i am not able to understand the complete program, what is get_transient and set_transients are :?
and What if i want to show random post from category whose id is 1 on Friday ?

<?php
if ( false === ( $totd_trans_post_id = get_transient( 'totd_trans_post_id' ) ) ) {
     $args = array('numberposts' => 1, 'orderby' => 'rand'); // Generate Random Post
     $totd = get_posts($args); // Get the Random post generated earlier
     $midnight = strtotime('midnight +1 day'); // Get the Time of next day from Today's Midnight
     $timenow = time(); // Current Time
     $timetillmidnight = $midnight - $timenow; // subtracting current time from Midnight (Time remaining in midnight)
     set_transient('totd_trans_post_id', $totd[0]->ID, $timetillmidnight); 
} else {
    $args = array('post__in' => array($totd_trans_post_id));
    $totd = get_posts($args);
}

foreach( $totd as $post ) : setup_postdata($post); ?>
    <div>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <?php the_content(); ?>
    </div>
<?php endforeach; ?>
2
Contributors
15
Replies
18
Views
5 Years
Discussion Span
Last Post by Xufyan
Featured Replies
  • To check if it's Friday, you could use the [URL="http://php.net/manual/en/function.date.php"]date[/URL] function: [CODE] if(date('N') == 5) { // You code here } [/CODE] Read More

  • Great. Please mark as solved :) Read More

0

It looks as though it is storing the time against the post, so that the same post is used for an entire day.

Another way to achieve something similar would be use the query:

global $wpdb;

$timestamp = strtotime(date('Y-m-d'));
$sql = "SELECT * FROM `wp_posts` WHERE `post_type` = 'post' ORDER BY RAND({$timestamp}) LIMIT 1";

$post = $wpdb->get_row($sql, ARRAY_A);
setup_postdata($post);

This is untested, but should work.

R.

0

So you are picking the post directly from database which is ordered as Random, right ???

but what does the line 6 is doing ? :/

0

Yes. And providing RAND() with a key, it will always return the same value when the key remains the same. Thus, using the timestamp for todays date as the key, the post will change once per day.

Line 6 is retrieving the data from the database. Line 7 sets up the post data so you can then call the_title(), the_content(), etc and it'll output the content for the post retrieved from the database.

You might actually want to store the original post value in a temporary variable and restore it when you've finished with the random post.

global $wpdb, $post;
$_post = $post;

$timestamp = strtotime(date('Y-m-d'));
$sql = "SELECT * FROM `wp_posts` WHERE `post_type` = 'post' ORDER BY RAND({$timestamp}) LIMIT 1";

$post = $wpdb->get_row($sql, ARRAY_A);
setup_postdata($post);

// Do what you want with the random post here

$post = $_post;

R.

0

I tried displaying a post but nothing is displaying with that code :?
what am i doing wrong ?

0

Post your code.

You obviously need to output some content, so if you just copied the above verbatim, there is nothing currently being outputted to the browser.

R.

0

I've done this

<?
global $wpdb, $post;
$_post = $post;
$timestamp = strtotime(date('Y-m-d'));
$sql = "SELECT * FROM `wp_posts` WHERE `post_type` = 'post' ORDER BY RAND({$timestamp}) LIMIT 1";
$post = $wpdb->get_row($sql, ARRAY_A);
setup_postdata($post);
the_title();
$post = $_post;
?>

nothing is output,
in meantime i am also reading about 'wpdb' here http://codex.wordpress.org/Class_Reference/wpdb
and so far i am understanding it :) as i am a bit newbie in MySQL/PHP this is why i am facing problems :)


I was trying to check if the day is Friday by this,

echo (strtotime("next Friday")

then i will compare it with the current day and if it gets true I will run the mysql query to show a random post from Cat Id=1 :)

Thanks to you....i was unaware about that wpdb thing before :)

Edited by Xufyan: n/a

1

To check if it's Friday, you could use the date function:

if(date('N') == 5) {
    // You code here
}
Votes + Comments
thanks :) Xufyan
-1

And try the following. Seems setup_postdata takes an object, not an array.

<?
global $wpdb, $post;
$_post = $post;
$timestamp = strtotime(date('Y-m-d'));
$sql = "SELECT * FROM `wp_posts` WHERE `post_type` = 'post' ORDER BY RAND({$timestamp}) LIMIT 1";
$post = $wpdb->get_row($sql);
setup_postdata($post);
the_title();
$post = $_post;
?>

Edited by blocblue: n/a

0

Thanks alot for the Help , here is the final code of mine,
the else part is working fine however when i set the day to 'Friday' , nothing appears. ?

<?php
global $wpdb, $post;

$_post = $post;
$timestamp = strtotime(date('Y-m-d'));
if ((date('N'))==5){
$sql = "SELECT * FROM 'wp-posts' WHERE 
(
term_taxonomy.taxonomy = 'category' 
AND 'term_taxonomy.term_id' = '495' 
AND 'post_type' = 'post' 
ORDER BY RAND({$timestamp}) LIMIT 1)";
}
else{
$sql = "SELECT * FROM `wp_posts` WHERE `post_type` = 'post' ORDER BY RAND({$timestamp}) LIMIT 1";}
$post = $wpdb->get_row($sql);
setup_postdata($post);
the_title();
$post = $_post;
?>
0

Well for starters, date('N') just returns the current date as a numerical value. Today is Wednesday, hence 3 is returned and 3 != 5.

Also, there isn't anything in the if statement apart from a SQL query. What are you expecting it to do? It will work without the if statement.

0

I know 3 isn't equals to 5 :/ ,
when I changed my System date to Friday then nothing is showing up but when i changed it to any other day it is working fine

what my if statement doing is,

when day = 5 then it runs different SQL query

Edited by Xufyan: n/a

0

That would indicate that there is a problem with the SQL query in your if statement. Have you tried it in PHP MySQL or in the MySQL console to see that it actually works?

0

Thanks alot for the Great HELP of you :)
problem solved :)
actually i had to Join to query in order to get posts from specific category and i've done this

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.