For a random offset we might try:
$ppp = 4;
$total = wp_count_posts()->publish;
$offset = $total < $ppp ? 0 : rand( 0, $total - $ppp );
$posts = get_posts( [
'posts_per_page' => $ppp,
'offset' => $offset
] );
Example:
Let’s take $ppp
as 4 and assume $total
is 6.
Then are three possibilities for the $offset
, namely 0, 1 and 2:
Nr Offset Selections
1 0 x
2 1 x x
3 2 x x x
4 3 x x x
5 4 x x
6 5 x
so
$offset = $total < $ppp ? 0 : rand( 0, $total - $ppp );
would give:
$offset = rand( 0, 6 - 4 );
or just
$offset = rand( 0, 2 );
Related Posts:
- Logged in user ID as post ID
- Displaying POST content with HTML tags and all
- Link on post title only if post have content
- How to make a image-size selected by default in Media upload – WP v3.5
- Hide main div if wp_nav_menu is empty
- How to draw media details for Isotope gallery using Photoswipe
- Add Useragent to the body class?
- Remove wp-mediaelement.css from wp_head
- add_filter to post-gallery and remove all ‘s?
- Set the transport of the Customizer ‘header_image’ core setting to ‘postMessage’
- show limited tags in an article
- delete an array element when its date expires
- Using a function written in my functions.php file within the header.php file
- Disable External Pingacks on WordPress Posts and Only Allow ‘Self Pings’
- Wrap each shortcode in array to div
- Warning: array_pop() expects parameter 1 to be array, boolean given
- Replace comment avatars and links at the same time
- Check if Product is in a Specific Category in Functions.php
- Adding ads code between comments
- Create Logout Link WordPress Admin Menu
- How to Display Image Meta underneath EVERY image in EVERY post
- Limiting woocommerce line_total decimal length
- Run a filter when a walker runs
- Shortcode content filter?
- Swapping wp_dropdown_categories function with wp_category_checklist
- Display fields as values in array from external SQL DB
- only show container with next/prev links if they exist?
- Populate dropdown with Child Pages based on Parent Page chosen
- How can I remove a function that has been added to wordpress with add_filter?
- JS file work only in index page
- Error in custom php function doesn’t exist
- Custom shortcode outputs plain text instead of HTML at top of post
- how can i use $_COOKIE[] + PostID to execute the below code?
- What’s the uses of wp_cache_set() or wp_cache_add()?
- Create Page To Count Comments For Each Post In WordPress Site
- Can anyone offer any help with this function?
- Shortcode with PHP issue “Undefined index”
- height should be set to auto to avoid pixelation in the post thumbnail function
- Can i put my shortcode inside a variable in php files?
- Moving code from theme header to functions.php
- Override a function defined in wp-includes/comment-template.php
- Show meta box only for default page template
- Can’t access site after making changes to the functions.php [closed]
- Best way to use a large array in function
- Setting user nickname and displayname to shortened email
- Download PDF after CF7 form submission
- Is it possible to define variables in a wordpress shortcode, and then call the shortcode using a specific variable?
- Custom meta box values are not getting saved for my custom post type
- Simple AJAX notification when the new post is added to the database
- Upload multiple files in randomly generated folder using wp_upload_bits
- Personalize checkout text
- How do I create a function that modifies a message in the wp-login.php file?
- Cant display an image via PHP in wordpress
- Template Loop – add switch case php
- get post id from wp_insert_post for get_template_part
- Adding an interior ‘hero’ area with some added php title customization
- Call to undefined function error in plugin
- How do I correct the code in php? [closed]
- Custom Taxonomy Select Menu: Setting default option value?
- How to add author’s posts link (HTML + PHP) inside a function to output it
- Creating customized php files in theme folder
- Unable to Call More than One Meta Box Output
- Menu not styling. New menus functionality giving me a headache
- How to set value selected using select2 jquery plugin
- Workaround for og:image unsupported webp image type, Yoast SEO?
- Disclaimer that will show every refresh of the page
- Dynamically switch file in get_template_directory_uri() | Function [closed]
- Using ACF Relationship field to set post type to draft or published status
- Providin exception to WordPress wp_nav_menu Custom CSS Classes
- Woocommerce My acount page
- Dropdown category field inside repeatable metabox
- Retrieve alt text by referencing the img src
- Ajax filter with loadmore button
- How can i avoid duplicate same post in wp?
- Child theme overirde template-tags in a theme built on underscores in inc/template-tags
- Delete post meta by serialized meta value
- Custom meta box is not displaying value showing tag as empty
- Menu not updating for logged in users after redirect
- Create a temporal post
- PHP/CSS: Shortcode won’t display correctly, and only displays in the head (before content)
- Add parent menu item in sub-menu in custom nav_walker
- Add class for the_post_thumbnail_caption()
- Getting page / post URL on publish and / or update
- Weird Behaviour: Not all WordPress Posts appearing
- Save the outputted image into the Media Library, with a different filename and extension
- Custom Taxonomy as Link
- How to fix blank pagination links?
- Adding ID to a Listing but not inside WP_Query while
- adsense code showing on random post thumb
- How to extend the page editor?
- How to return a Boolean for a page that is a subpage thru its name?
- I need a button to appear when not loged in and another when logged in, I need help fixing code PLEASE!
- Undefined variable notice [closed]
- How to hide .js files on wordpress website using php or wp plugins or any? [closed]
- WP Custom tables query
- How do I get a function to work in single.php
- WordPress different custom tag being displayed in on tag list
- Exclude a category ID from the following function
- Add text below WooCommerce short description if metabox value is true
- ACF number less than comparison not working