Pro tip – don’t custom query unless you need to (you don’t). And never use the guid
field.
if ( is_singular() && $cats = get_the_category() )
$cat_id = $cats[0]->term_id; // Category ID for current post
elseif ( is_category() )
$cat_id = get_queried_object_id(); // Category ID for current archive
else
$cat_id = 0; // No cats
$posts = get_posts(
array(
'posts_per_page' => 1,
'orderby' => 'rand', // Here's our random magic
'cat' => $cat_id,
)
);
if ( $posts ) {
$random_url = get_permalink( $posts[0]->ID );
}
Related Posts:
- How to add category to: ‘wp-admin/post-new.php’?
- Assign category to new post via URL
- Can I show category name in url for only one of my categories?
- Does having category name in permalinks affect SEO when having a post in multiple categories?
- How to add a ‘News’ section to specific posts in WordPress
- Querying posts from current category, using a variable as array argument
- How can I display a specific number of post in a category via a url
- Get Posts Under Custom Taxonomy
- How to display related posts from same category?
- How to show related posts by category
- Access post from post id in URL
- How to Check if a Page Exists by URL?
- Including categories in search results
- Use category base slug in posts’ permalink
- Different post sort order within different categories
- Count how many posts in category
- Custom Single Post By Category
- Setting Custom Sort Order of Posts within a Category
- How to get the number of posts in a selected category?
- MySQL Query to Retrieve Category from wp_posts
- Different number of posts in each category
- Display all posts from specific categories on a page
- Strategy for handling hierarchical pages with empty parent content
- Display the latest post from a category in a page
- How to show list of posts by author and category?
- Exclude posts that only have the ‘Uncategorized’ category [duplicate]
- Category checkbox list tree changes when editing a post
- How to add metabox for post of specific category
- Convert custom fields to post categories
- Manipulate post category after time
- Show related posts by category but ignore one category
- Open WordPress ‘Add New Post’ admin page with parameters set via $_GET
- How to sort posts by last name (2nd word) on ONE category only?
- How to disable Uncategorized category URL?
- Add role that restricts user to post in specific category
- How to get Category Id from Post Id ?
- Count posts in category including child categories
- Query posts from current year
- How do you get all the urls of images attached to a post?
- Getting Position of a post in a category
- Display only posts from a specific parent category
- How to control which category will be picked for the slug of a post?
- Categories box not showing on post edit pages
- get_the_term_list by hierarchy order
- How to change the layout and styling of posts according to their category in WordPress?
- List only child categories a post is in, of a specific parent category
- Add default content to post (for specific category)
- Query only Posts from Both of Two Category?
- Show Custom Taxonomy Categories, Listing of Posts, and Single Post via AJAX
- Add custom field to all posts in specific category
- Exclude category on blog list page
- display sub categories assoccited with each post in a category archive page
- is there a better way of combining this?
- Display most recent post in category instead of archive?
- How to allow slashes in single posts when i click in the edit permalink button
- How can I specify a category post on my home page
- Single.php Active Category Class
- Most efficient way to list all categories and display a post for each of them?
- Archived Posts in Wrong Category
- Show One Category on Post
- Possible to create a permalink to sort with meta_key?
- how to grab first link in post… and of course call it
- Use template of parent category for single post sub-categories
- prevent showing posts of an specific category in admin posts section
- How to choose a sort order (for posts) per category? (ideally when creating a new category)
- wp_list_categories link to first post of category instead of calling taxonomy template
- How to search only by post title and category?
- Add custom field to category of custom post type
- WordPress Child Category Display All Posts
- Post in Multiple Categories to stay in current category (permalink, next previous post link)
- get_post_type_archive_link(‘post’) returns current category archive
- Display all posts under child sub category in sidebar on post page?
- How can I extract the URL of a link from a post?
- Do I use custom post type or something else?
- Can I hide a specific post from latest posts page?
- How do I get the slug of a custom taxonomy category of a post?
- What Defines What Category A Post Picks (if in multiple)
- Featured Posts for Category Pages
- Auto set Post to Specific Categories
- Limit function to specific post category
- Permalink Settings: optional settings doesn’t apply to editor
- How to show post(excerpt) from specific category on wp page?
- How to Add ‘Post Categories’ to the Dashboard Sidebar
- New blog template to display only one category
- Create a separate JS application for an individual post?
- Authors in menu, template list post by author
- Get user’s most published categories
- Show all post titles
- How only display all post related to category
- WordPress – Admin Manage Posts – Multiple Filters by Parent Category
- Custom Theme Want To Remove One Category From All Displays
- Add Category name to Post Title (h1)
- WordPress menu that shows all posts in a certain category like w3schools
- Conditional that checks if a current category has any subcategory with posts
- Divide Loop Into Days & Categories
- Posts are not showing up on particular category
- How can I list random authors from current post category?
- How to set post expiration date and time and move the page to archive after expiration [closed]
- Add category to all post by using sql query?
- Display posts in alphabetical order for a particular category