0

How would I go about editing this code for displaying a random post from a category for 24 hours, then switching to another random post? This would be to show a Tip of the Day.

if ( ! function_exists( 'cb_breaking_news' ) ) {  
    function cb_breaking_news(){

        $cb_breaking_filter = ot_get_option('cb_breaking_news_filter', NULL);

        if ($cb_breaking_filter == NULL) { $cb_breaking_cat = implode(",", get_all_category_ids());  } else { $cb_breaking_cat = implode(",", $cb_breaking_filter); }    
        $cb_breaking_args = array( 'post_type' => 'post', 'numberposts' => '6', 'category' => $cb_breaking_cat, 'orderby' => 'rand', 'post_status' => 'publish', 'suppress_filters' => false);
        $cb_news_posts = wp_get_recent_posts( $cb_breaking_args);  
        $cb_news = NULL;
        $cb_news .= '<div class="cb-breaking-news"><span>'. __("Tip of the Day", "cubell") .' <i class="icon-long-arrow-right"></i></span><ul>';

        foreach( $cb_news_posts as $news ) {
            $cb_news .= '<li><a href="' . get_permalink($news["ID"]) . '" title="Look '.esc_attr($news["post_title"]).'" >' .   $news["post_title"].'</a> </li> ';
        }

        $cb_news .= '</ul></div>';

        return $cb_news;
    }
}

I found some code about it online although, I can't seem to get it to work with this. The webpage just goes blank.

2
Contributors
1
Reply
21
Views
3 Years
Discussion Span
Last Post by cereal
0

Hi,

you could use an external script and run it daily with cron job to set up a meta key in wp_postmeta, the name can be post_of_the_day, the value a Unix timestamp. An example:

<?php

    # config
    $dbhost = 'localhost';
    $dbname = 'database_name';
    $dbuser = 'database_username';
    $dbpass = 'database_password';

    $pdo = new PDO("mysql:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass);

    $pdo->beginTransaction();

    $stmt = $pdo->prepare("SELECT id FROM wp_posts as p WHERE p.id NOT IN(SELECT post_id FROM wp_postmeta AS pm WHERE p.id = pm.post_id AND pm.meta_key = ?) ORDER BY RAND() LIMIT 1");

    $stmt->execute(array('post_of_the_day'));

    if($stmt->rowCount() > 0)
    {
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        $stmt2 = $pdo->prepare("INSERT INTO wp_postmeta (post_id, meta_key, meta_value) values(?, ?, UNIX_TIMESTAMP())");

        $stmt2->execute(array($row['id'], 'post_of_the_day'));
    }

    $pdo->commit();

With the above you select a random post between those not used as post of the day. Then change the parameters of $cb_breaking_args to select only one post and order by meta_value:

$cb_breaking_args = array(
    'meta_key' => 'post_of_the_day',
    'post_type' => 'post',
    'numberposts' => '1',
    'category' => $cb_breaking_cat,
    'orderby' => 'meta_value',
    'order' => 'DESC',
    'post_status' => 'publish',
    'suppress_filters' => false
    );

$cb_news_posts = wp_get_recent_posts($cb_breaking_args);

If it works fine you will get the post selected by the external script. Then, the meta key post_of_the_day can be used to create a list of all these posts.

The best would be to use a datetime column for the meta_value, but I'm not sure it is possible to alter or add columns to WordPress and make it work without too many changes in the core code. For this reason I chose a Unix timestamp instead of a classical date, it should be ordered correctly.

Important Note: this is not tested, I don't have a Wordpress installation and I don't know if it can work correctly, but the queries are mostly pointing to indexed keys so it should be fast.

Reference:
* https://codex.wordpress.org/Database_Description#Table:_wp_posts
* https://codex.wordpress.org/Database_Description#Table:_wp_postmeta
* http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
* http://codex.wordpress.org/Function_Reference/get_posts

Let us know if it's ok, otherwise if you still want to use the code you were referring to, paste it here. Bye!

This topic has been dead for over six months. 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.