• Hey Guys.

    Looking for advice / assistance.

    I have created a custom post type, I would like to generate a random post from the CPT on page, i.e when a client clicks a button (Randomiser) , the post type is automatically generated in section/ on page without moving to a new page.

    The page I need help with: [log in to see the link]

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

    (@bcworkz)

    Are you actually creating a new random post that’s saved in the DB? Or do you just want random content to appear in a particular section of the page (or both)?

    Wouldn’t saving random posts on each button click end up filling up your DB with random content that serves no purpose?

    Anyway, you can use JavaScript to cause random content to appear in a page’s section whether the same is saved or not. To save post data to the DB, use JavaScript to post to the REST API. To have the API save data, the current user must be properly authenticated. An easy way to do this is with the recently introduced application passwords.

    Thread Starter robbibosch

    (@robbibosch)

    No… I have created custom post type called “Book Quotes”- with the objective to randomise posts(“Book Quotes”) that are created in the Custom Post Type.

    Theses are pre-created, and when the visitor clicks the button the custom post type is displayed on screen without navigating to another page to view post.

    Moderator bcworkz

    (@bcworkz)

    Oh OK, you just want a random custom post type displayed on every button click. Your use of the word “generate” in your OP threw me 🙂

    Is there a lot of other content on the page? The simplest solution would be for the page to display a random post on initial load. Clicking the button would simply reload the entire page which would then contain another random post.

    If there is a lot of other page content, that approach isn’t very efficient. More efficient but harder to implement would be for the button click to invoke some JavaScript that fetches a random post through the REST API. The returned post data can then be used to replace the existing post content.

    Thread Starter robbibosch

    (@robbibosch)

    There is alot of content on the page – so the first option is not going to work.

    “A button click to invoke some JavaScript that fetches a random post through the REST API. The returned post data can then be used to replace the existing post content.”

    This is more along the lines of what I am currently searching for a solution for.

    Moderator bcworkz

    (@bcworkz)

    Maybe one of these plugins would suit your need:
    https://ww.wp.xz.cn/plugins/search/random+post/

    If you’re keen to develop your own solution, the source code from one of those plugins could serve as guidance towards your own efforts.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Random Generator – Custom Post Type’ is closed to new replies.