• I am trying to randomize my posts on the homepage. I prefer not to use a plugin unless it keeps my theme’s formatting. I’ve tried a few “solutions” I found on Google. One solution I tried, works with another theme, but not my current theme.

    add_action('pre_get_posts','alter_query');
    function alter_query($query){
        if ($query->is_main_query() &&  is_home())
            $query->set('orderby', 'rand'); //Set the order to random
    }

    The above code works fine, for one theme. But on my current theme, I get a syntax error message.

    If there’s a plugin that simply randomizes the homepage and doesn’t change the current style, please let me know. Thank you.

Viewing 6 replies - 1 through 6 (of 6 total)
  • That code should work on any theme, but I suppose your theme could be doing something strange to the home page query already. But maybe you introduced a syntax error when trying it with your theme?
    Do you have your home page set to show Latest Posts, or Static Page?
    (The is_home() check with return true only on the page that is Latest Posts, whether that is home or a separate page.)

    The problem with showing random posts is the pagination. If a visitor sees 5 random posts on home page and clicks on the link for next page, they could see some of the same posts since it’s random. The first query doesn’t carry to the subsequent pages; a new query is done for each page.

    Thread Starter faulknordonald

    (@faulknordonald)

    I doubt it was me. I can remove that and add any other piece of code and it works. Plus, I know PHP quite well, just not familiar with a lot of WordPress-specific functions. I understand very well how pagination works. I don’t intend to use pagination, I just want random posts on the homepage (no pagination). And yes, my homepage is set to latest posts (WordPress core should introduce random posts to that lol). I actually started this website from scratch (I coded it myself, no WordPress), but then decided to switch to WordPress as it’s less time consuming.

    OK, well, it seems like if it said syntax error, then you introduced it, especially if the theme works fine before you added the code and after you removed it.
    Just so you know, even if you don’t show a pagination link, WP will still process them (page will be shown if that URL is visited).
    I searched for random post plugins, but they are mostly widgets. It doesn’t make sense to make a whole plugin for that little piece of code you have (which is all you need).

    Moderator bcworkz

    (@bcworkz)

    We can confirm the code you offered is correct for typical WP themes. When your theme deviates from common practice it’s likely a theme where we cannot access its source code so we’re unable to advise further. I recommend you seek assistance from your theme’s dedicated support channel if you’re unable to achieve the results you want yourself.

    Thread Starter faulknordonald

    (@faulknordonald)

    Ok, thank you bcworkz and Joy. And I do believe it’s something wrong with the theme itself. I can add any other piece of code to functions.php, just not that piece of code. I’ll contact the theme’s author, thanks again.

    Thread Starter faulknordonald

    (@faulknordonald)

    BTW Joy, I am currently using a plugin with a shortcode whereas I can place the random posts on a page. So, I created a page using the shortcode, and it looked nothing like what I wanted it to. So, I had to heavily modify the plugin’s “posts.php” page to suit my needs, then I switched my homepage to that static page.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Random Posts on Homepage’ is closed to new replies.