You can use wp_get_post_categories to get the categories from your posts. Then you can use array_rand to get one random category from that array.
Check out these links on how to use wp_get_post_categories and array_rand to suit your needs:
https://developer.wordpress.org/reference/functions/wp_get_post_categories/
https://www.php.net/manual/en/function.array-rand.php
Related Posts:
- How to know if get_posts() failed?
- Get category slug and display it on a query_post
- How to show next Post Thumbnail image in WordPress using current post id
- How to add paging to query
- Am I using get_posts wrong?
- Is there any need to use both wp_reset_postdata and wp_reset_query together?
- How to query the latest 5 posts and sort them by title?
- How to reset post data properly
- Custom Post Type Query issue
- I have WordPress setup on primary domain, how do I set it up on its subdomain using the same database?
- How to get url image page the right way?
- show posts in different styling
- Querying by post category slug with core/query block markup
- How to add custom css file in theme?
- CSS not updating in browser when I change it
- Is wp_is_mobile() effective?
- Display Menu Name using wp_nav_menu
- What questions do you ask when asked to give an estimate on theme development?
- How to remove dashicons.min.css from frontend?
- What’s the difference between home.php and index.php?
- get_template_part vs action hooks in themes
- Get url of thumbnail from the media uploader
- How to: Update live sites theme from version control repo via plugin or library? [closed]
- Adding items to page template dropdown on Page Edit Screen
- How to use logout function on custom menu link?
- Hide certain pages / posts on wp-admin, show custom filter
- use add_action(‘wp_head’) in a widget for generating dynamic CSS styles
- Problem with registering menus – What to do when other solutions aren’t working?
- Front End Post Submit Form
- WordPress Theme customisation CSS
- Displaying wp menus by name without using theme locations
- Adding dashicon fonts to the admin of pre 3.8 installs
- Why does this loop only work on the homepage?
- Nesting Functions within Functions
- Do I really need the div class entry?
- How to manipulate “add media” pop-up
- Does meta_query work within get_posts array?
- How to determine if post has widget content?
- How to show more than 5 posts?
- How to Take Ownership of a Theme
- Custom Script Section Only Echoes Text
- Best way to start becoming a wordpress developer
- Why wp_head() function not loading style.css?
- How to make content editable from admin page?
- Set multiple templates per post
- Place the page title into the short code
- Redirect Old .php URLs to New WordPress Page
- create-guten-block in wordpress theme?
- WordPress navbar with logo in middle [closed]
- Displaying recent post excerpts on static front page
- Is the current Theme version number cached somewhere?
- PHP Template way of coding for wordpress theme development
- Which html elements should be styled in wordpress theme
- Custom form in theme template displaying internal server error upon submission
- WordPress Media Uploader in page template (On Front-end)
- HTML added to editor when deleting shortcodes in Chrome browser
- Trackbacks not displaying
- Widget Option is Missing
- How do I use a color from theme options?
- If you were to start using a WordPress framework today, which one would you use? [closed]
- How to include a file only on dashboard widgets page?
- WP_Query $gallerypage->the_post() giving Undefined offset: 0
- How do I conditionally defer scripts based on the page name?
- How to place POST TITLE any location inside the posts page
- Do I need to re-import Susy to work with it in a child theme? [closed]
- Valid SQL query not returning results
- Change Image Sizes for Mobile Theme
- Recommended sidebar / content widths
- Trouble in creating a custom template on twentyeleven theme
- language support in a custom urdu theme
- Can I include get_posts in this array?
- Understrap-child conditional js script
- Does single webpage do not need navigation to create a slug in permalink?
- Posts Page shows Classic Editor interface not Gutenberg
- WordPress theme doesn’t read my translations from pt_BR.po file
- Get gallery images description not work for some images id
- How to get full native language instead of iso?
- Move the social media icons to the left of a WordPress nav menu for Soledad child Theme
- Is a multipurpose theme an alternative to modifying or creating a theme from scratch?
- Two instances of the theme folder in the URL
- I want to change the author name
- Does any JavaScript file load automatically for index.php file?
- Display content on Single page
- Ordering Posts by parent category, name ascending
- Horizontal Navigation
- Trouble creating custom sanitization function when uploading video files
- How to store and retrieve the attachment alignment?
- Custom image size doesn’t work
- Paginated WP_Query doesn’t return 404’s, even when posts don’t exist
- The content not wrapped in paragraph tags with get_page_by_title()
- How to assign the default file at “Appearance > Editor”?
- Not Found when using activity stream as front page with BuddyPress
- How customizable is a self-hosted WordPress blog compared to a Blogger blog?
- Why we do need wp_enqueue_script() function?
- Is it possible to use “wordpress.org Theme Handbook” look&feel as a theme in my own site? [closed]
- sanitize_option_{$option} filter returns null
- WP Customizer get control value on change
- Execute javscript when theme customizer loads (autosave issue)
- How to set the margin on an innerBlock in a block variation?
- WordPress theme.json: Why doesn’t “contentSize” work