• Hi,

    does someone know how I could order the posts randomly and change this order every second day?

    This is how I get the posts:

    query_posts($query_string);
    if (have_posts()){...

    If I use &orderby=rand it gets mixed each time the page is refreshed, also on changing the pages.

    Would be great if I could order it via menu_order and change the menu_order every second day via an SQL-Update.

    How yould I do this?

    Thanks!!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    If you provide a seed argument for the mySQL RAND() function, you should get posts in the same random order on each request. The query_posts() ‘rand’ argument cannot handle this, but you can insert it with the ‘posts_orderby’ filter.

    You could store the seed in the options table and create a scheduled event to change the seed every other day.

    You may be aware that using RAND() on very large tables is computationally very expensive. I’m unsure if shuffling the results in PHP is any better. Depending on your situation it may or may not be something to investigate.,

    Thread Starter Ploeve

    (@ploeve)

    Hi,
    thanks, yes, that is what I need.
    I’ve just some 50 post to sort, so I guess it is okay.
    How can I change an option via a scheduled event?

    Can I do it like this in the functions.php of the theme?

    add_action( 'wp', 'prefix_setup_schedule' );
    /**
     * On an early action hook, check if the hook is scheduled - if not, schedule it.
     */
    function prefix_setup_schedule() {
    	if ( ! wp_next_scheduled( 'prefix_hourly_event' ) ) {
    		wp_schedule_event( time(), 'hourly', 'prefix_hourly_event');
    	}
    }
    
    add_action( 'prefix_hourly_event', 'prefix_do_this_hourly' );
    /**
     * On the scheduled action hook, run a function.
     */
    function prefix_do_this_hourly() {
    	// do something every hour
    }

    thanks

    Moderator bcworkz

    (@bcworkz)

    I don’t have any experience using scheduled events from themes but I see no reason it will not work. Even if not, creating a simple plugin is not a big deal.

    You will of course only need a recurrence of every other day instead of hourly. The longest default period is daily, but I understand you can specify longer periods with the ‘cron_schedules’ filter.

    Besides adjusting the interval, you just use update_option() where the // do something every hour currently is. Simply storing the current timestamp should be adequate for a seed.

    If you haven’t already done so, do test the seed concept before spending any time on scheduling. I’ve every reason to believe it will work, but I’ve not personally observed this so there is a tiny bit of doubt.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Randomize order of posts every second day’ is closed to new replies.