This happens because you use a foreach
loop on post categories. You can display random posts from the first returned category by getting rid of the foreach
loop and using $categories[0]->term_id
as your category
in query args.
You can also try to grab all the category IDs for all your categories and include them as comma separated list. This will return 8 random posts from all categories associated with your post. Read more in the ‘Paramaters’ section of get_posts
function.
Related Posts:
- setup_postdata() does not seem to be working?
- Regenerate Slugs From Title of Posts
- get_posts – get all posts by author id
- Why get_posts are only showing five posts (retrieved by assigning a category to them?
- WordPress get_posts by category
- Alternative to get_posts() due to multithreading cache crash
- How to restore deleted pages/posts?
- Sort “get_pages” by menu order not ordering
- get posts based on meta value of the author
- date_query not returning some posts in date range
- Get the exact SQL query that get_posts() generated
- How do you get posts by meta_query using the JSON API plugin?
- Why favour the standard WP loop over iterating over (new WP_Query())->get_posts()?
- Query posts ordering by title, but ignore ” and special characters
- get_posts inside cron
- get_posts not honoring post_status
- get_posts returning empty array
- get_posts only children from certain parents
- Problem with get_posts, tax_query and counting the number of posts
- Filtering custom post type query
- data returned from get_post($postId) have different keys than wp-json/wp/v2/posts/{postId}
- meta_query date and time comparisons
- get_posts from post x(offset=>x) to end
- With get_posts(), how can I put a category as a variable
- Serialized array, grab specific posts with meta_key/meta_value[0]->is_featured
- How do I combine these 2 queries
- Increase the page size of the WordPress REST API
- How to generate a list of posts published on current day?
- Get latest posts from multiple categories
- get_posts() returns all posts rather than the ones specified with ‘post_author’ =>
- Query random post from different categories
- Get_Posts, only if in both categories
- get_posts() excluding all children of a specific post/page
- If clauses in get_posts query
- excluding current post from get_posts
- How to query posts to include specific pages of one post type, and all pages of another post type?
- Only retrieve posts where post_excerpt has been filled out
- Can I use numberposts=-1 and offset together when using get_posts()?
- Ordering posts in get_posts
- I don’t arrive to do order_by title when i have a conditionnal year in a request
- Get posts inside Get terms problem
- Ho to add get_relative_date and post_except to this get_latest_post query?
- How to update all posts but the current one (post__not_in not working?)
- Only showing the_date and the_excerpt for first entry in get_posts
- get_posts() not working in functions.php
- get_posts not working as expected
- Getting only a specific post type with get_post?
- Display post details by post ID
- What is the p parameter in a get_posts() parameter array?
- Exclude post format from get_posts
- Memory issue with get_posts( ) function
- get_posts() using an array of post ids [closed]
- Iterate over get_post_meta() result
- How can I show the author’s latest post with title?
- Unique Problem with the php date and get_posts
- get the post_date of ID
- Get posts by language in Polylang plugin
- Get posts in between specific ID’s
- Auto-change Post Status on First Page Load
- Don’t submit post if post title exists
- Why would get_posts be ignoring posts with 2-digit ids?
- Not able to get random post
- Order custom post type by custom field (created by PODS) via PHP
- Override Blog pages show at most with get_posts
- SQL query generated by WP_Query is not the same what request echoes
- Calling setup_postdata() causes “Call to undefined function get_userdata()” error?
- ‘child_of’ in get_posts not working
- Custom Post type Query post is not working
- Fetching All Posts (200+) in a Category – Performance Issues?
- How do I get only a certain number of images from a post (minus the featured image)?
- Modified home page query does not yield expected results
- Sort get_children by menu_order
- Working with get_posts parameters/arrays/strings
- Getting post by specific date in Advance Custom Field
- Select objects from get_posts()
- Divide the list into two columns (get_posts)
- Using post title as term in query, only working with single word titles
- Single random post in homepage
- WordPress echoing content in seemingly random place
- How to use get_posts to filter against a single post id?
- Why set cookie from URL parameter doesnt work all the time?
- How to get a custom post with the largest ID (not the last post by date)
- Is there a way to extract a single post better than using get_posts()?
- Array merging multiple get_posts throwing Undefined offset error
- Custom Category widget
- Zero posts returned with get_posts() on edit.php, but posts are returned on post.php
- Why does ‘meta_key’ return an empty array in get_pages
- Better way to query posts?
- Using get_posts, I need to use a combination of OR & AND relations
- get_posts array ‘between’ not behaving
- How to Retrieve Post ID of another page
- How To Use get_posts & get_the_post_thumbnail Outside The Loop
- WordPress – List Sub Categories and Sub-Sub Categories, With Posts
- get_posts() and ‘posts_per_page’ arg
- Show posts in a list separated by day
- Sorting posts by specific order
- Display all Post titles with Category and Tag
- Intergrate WP into website
- Latest post on specific category and custom css
- Display post’s description and caption