Support » Everything else WordPress » display a random PAGE in a widget

  • Hello

    I would like to display a random PAGE in a widget (if possible with photo, title and link).

    I find a lot of plugins that can display post randomly, but not for pages.

    Do you have an idea or a solution?
    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • You could use the plugin you like for posts, and make a copy of it. Then change the code where it is querying the database. It should be just a change of post type, from “post” to “page”.

    Moderator bcworkz

    (@bcworkz)

    You could probably still use one of those plugins and alter the query they use to get pages instead of posts. Assuming they use the WP_Query class, this can be done by hooking the “pre_get_posts” action and setting the “post_type” query var to be “page”.

    The trick is to only modify the widget’s query and not any others. It’s likely it’s the only query where the posts_per_page value is 1 and the orderby query var is “rand”. By checking for characteristic query vars before changing the post_type query var, you avoid altering the wrong query.

    If it wasn’t already obvious, this approach involves custom coding. If you’re unable to do so, nor are able to hire someone to do so, maybe someone else will have a suggestion for an existing plugin, but it sounds like you’ve already done a fair bit of searching on your own without success.

    Thread Starter loupiloop

    (@loupiloop)

    Thanks for your help and answers !

    I finally fond this plugin which do the job with a parameter !
    https://fr.wordpress.org/plugins/random-post-on-refresh/

    [random_post_on_refresh post_type=”page” show=”title, excerpt” ]

    bye

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.