• Hello,

    I hope everyone is safe.

    Ok, so I need a little advice.
    Basically, I have a blog in which I would like a button that will lead to random posts of my WP website. But I also would like to give the choice to my visitors to filter for what type of posts (by the category type) they would get random posts.

    For example:

    Let’s say that my blog have 5 categories of posts: dog, cat, bird, fish, wolf.

    We will have the “Random” button, and next to it, a choice by checkbox for each of these 5 categories. If the user check the box for “dog” and “cat” only, after clicking the “Random” button, a random post only from the “dog” or “cat” category will be loaded. Not “bird”, “fish” or “wolf”.

    Currently, I temporarily set 5 random buttons, one for each category. If I click on the one associated with the “dog” category for example, a random post from the “dog” category will appear. So of course, I would like to replace that system with one button only, and the 5 checkbox.
    Preferably, I would like the script to FIRST randomly choose one of the category for which the checkbox have been checked. And SECONDLY, to randomly choose a post for this category.

    Here is the code I have for the 5 buttons.
    Can you help me modify it to replace it with the checkbox system and one “random” button?

    (NOTE: I am using the plugin “Redirect URL to Post” to generate the url redirecting to a random post)

    
    [su_button url="https://website.com/?redirect_to=random&cat=2" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]DOG [/su_button]
    <br>
    
    [su_button url="https://website.com/?redirect_to=random&cat=5" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]CAT [/su_button]
    <br />
    
    [su_button url="https://website.com/?redirect_to=random&cat=6" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]FISH[/su_button]
    <br />
    
    [su_button url="https://website.com/?redirect_to=random&cat=7" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]BIRD [/su_button]
    <br />
    
    [su_button url="https://website.com/?redirect_to=random&cat=8" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]WOLF [/su_button]
    <br />
    

    I hope my explanation was clear.

    Take care.

    • This topic was modified 6 years, 1 month ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 15 replies - 1 through 15 (of 21 total)
  • @vmackey1

    Can you currently create a single button that links to more than one Category?
    And if so, what is the syntax for that ?

    Thread Starter vmackey1

    (@vmackey1)

    Thanks for your reply.

    So currently, I can either create a button for ALL categories, the syntax being:

    [su_button url="https://website.com/?redirect_to=random" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]ALL CATEGORIES [/su_button]
    <br />

    or for just ONE category:

    [su_button url="https://website.com/?redirect_to=random&cat=2" style="flat" background="#2D9600" size="20" center="no" rel="nofollow"]DOG [/su_button]
    <br />

    But I am not sure how to do so for a partial selection of categories.

    • This reply was modified 6 years, 1 month ago by vmackey1.
    • This reply was modified 6 years, 1 month ago by vmackey1.
    • This reply was modified 6 years, 1 month ago by vmackey1.

    @vmackey1
    So weird – I can’t think of a single instance I would want to do this!

    But anyway, you got me intrigued, so based on some quick covid afternoon boredom :

    This works (kind of).
    Let me know if it’s what you were thinking.

    https://gowildcats.ca/random-post/

    – I created the form and inserted a handful of available categories
    – user select 1 or more categories
    – added a form action in the functions file to alter the submit to read the value(s)
    – created a second function to pass in the post_type, orderby (random), number_of_posts, and then category__in to store the array :
    https://developer.ww.wp.xz.cn/reference/classes/wp_query/#category-parameters
    – used the regular wp_Query object to query the posts :
    $the_query = new WP_Query( $args );
    – added a shortcode in the functions file, and inserted that onto the “Thank You” page

    If you choose a couple of categories, submit, just keep refreshing the page and you’ll see how they display in the random.

    @vmackey1

    Just to be clear, did you already try to add pre-defined categories by passing in the other category IDs into the same string, like this :

    [su_button url=”https://website.com/?redirect_to=random&cat=2&cat=4&cat=1” style=”flat” background=”#2D9600″ size=”20″ center=”no” rel=”nofollow”]DOG [/su_button]

    Thread Starter vmackey1

    (@vmackey1)

    Haha. Who said boredom was counter-productive 🙂

    That’s excellent! However, the system I needed would be to be redirected directly to a random post (from one the selected category). Not display a list of random posts.

    For example, if ‘Atom’ and ‘Bantam’ are selected:

    First, the script will randomly choose one of these two categories.
    And secondly, the script will randomly choose one the posts of that chosen category.
    And then redirect to that page.

    You see what I mean?

    Thread Starter vmackey1

    (@vmackey1)

    Yes, I have tried this, but it is only displaying random posts for the last category set in the code.

    For example, for:

    [su_button url=”https://website.com/?redirect_to=random&cat=2&cat=4&cat=1” style=”flat” background=”#2D9600″ size=”20″ center=”no” rel=”nofollow”]DOG [/su_button]

    I am only getting post for cat=1.

    @vmackey1

    Okay re: onPageLoad, that’s no problem to modify it like that.

    What plugin are you using? It may be easier to just modify what you already have.

    Thread Starter vmackey1

    (@vmackey1)

    Thank you!
    So for the redirection to random post, I am using the plugin “Redirect URL to Post”. It is that plugin that provide me with the URL: https://website.com/?redirect_to=random&cat={category} to generate random post.

    @vmackey1

    FYI, since the categories are placed into an array, you just need to separate them by a comma to include more than one category :

    https://website.com/?redirect_to=random&cat=2,1,4,8

    Thread Starter vmackey1

    (@vmackey1)

    Indeed it works. I appreciate the tips.

    @vmackey1

    Still doesn’t solve your form request.
    Are you still going to implement the input form ?

    Thread Starter vmackey1

    (@vmackey1)

    Yes indeed. This is necessary considering the layout I am looking to set for the site. It will be the way you presented it in your example. One submit button, and the list of categories to check/uncheck.

    @vmackey1

    Do you already run GravityForms ?

    Thread Starter vmackey1

    (@vmackey1)

    No I don’t. I see that it’s not a free plugin. Is it a necessary one for what we need to do?

    Thread Starter vmackey1

    (@vmackey1)

    (PS: I don’t know if you are a developer that accept projects currently, but let me know if you are. There’s more work where it comes from 🙂 )

Viewing 15 replies - 1 through 15 (of 21 total)

The topic ‘Create redirection to random post system’ is closed to new replies.