I think instead of using PHP rand
you’re better off limiting ( if you need to ) using posts_per_page
and then getting a random set using orderby => 'rand'
.
Full List of Ordering Parameters
So if I were to edit your query it would look like this:
$args = array(
'post_type' => 'attachment',
'meta_key' => 'on_front_page',
'meta_value' => '1'
'orderby' => 'rand'
'posts_per_page' => 6
);
$slides = new WP_Query($args);
$total_images = (int) $slides->found_posts;
if( $slides->have_posts() ) {
while( $slides->have_posts() ) {
echo $post->ID; // The Attachment ID
}
}
Related Posts:
- Query multiple custom post types in single loop
- How to add div blocks after certain set of post
- How to overwrite orderby with add_query_var
- Inject post (from specific category) between posts in Loop
- Build a content and excerpt grid loop with paging and options for # of posts
- How to create a WP_Query to search the Title or Tag?
- Alter secondary loop to exclude posts from current page category
- Specific Loop For 2 Within Each
- Display related products with custom output
- spliting posts into two columns
- have_posts() execution failure
- Why is variable not working on custom sql query using wpdb?
- Using foreach loop breaks
- Create WP_Query to search for posts by their categories or their parent/child categories
- WP Query with custom Shortcode
- Increase offset while looping
- How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field
- WP_Query fails despite having 1 post
- unable to use ‘new WP_Query’ in AJAX call
- wp_query get the 2nd post
- How to get post ID in a Page?
- Display an image if odd number of posts in grid
- get_the_ID() in the footer returns wrong value
- WordPress query undefined offset in loop
- Issue with custom loop in Archive page
- How to display SQL query that ran in WC_Order_Query?
- Get all posts as an array ID => Name
- How to hide posts of a specific custom category in WordPress?
- WP_Query multiple post results
- Do not duplicate posts with multiple categories in multiple loops
- Infinite Loop – WP_Query
- Dividing the loop to style post differently
- Limit tags shown in post
- How to extract information from a wp_query result?
- Echo array value
- Search.php gets metadata from first post
- the_post(); prints out style text into my HTML?
- Search AJAX Filters – Multiple Query Loops Into One Loop (Optimization)
- Wp Query : Order by distance lat,lon
- How can I modify this code to make the search box include tags and meta
- Display articles with a different template in the home page | Solved |
- in_array not working on dev server but works on localhost
- How to display sticky post always at the top (before regular post) in wordpress?
- wordpress query returning unexpected results
- Making list of posts with chosen description
- Query doesn’t display text data with apostrophes
- My query keeps looping infinitely ! how to stop it?
- Efficient way of querying for a “fallback” post?
- How can I display a query in a page?
- For each 3 posts, show a different post type
- Add post class in custom loop (WP_QUERY)
- Create a hierarchical loop at predefined markup requirements
- Problems With Query and/or Template Part and/or PHP
- Advanced Taxonomy query, with 3 post types
- Why does a meta_query break this WP_Query?
- Does wp_query and query_posts affect website performance? [duplicate]
- WordPress post pagination on custom template not working
- Pagination in category.php not functioning
- wp_query on search results page showing all results every time
- Show post like this image in my newssite [closed]
- Three different queries on same page
- How to use a conditional statement in a post loop but not count towards the “posts_per_page” if false
- How do I exclude the current post from the upcoming post query
- How do I display WooCommerce products in my query to rows of 3?
- How to get posts by a certain author in inner loop using outer loop variable or post title WordPress
- How do I use WP_query with multiple post IDs?
- is_page() not working from within a plugin
- Passing JSON data from WP Query into AJAX causing NULL errors
- How to order posts by modified date without using ‘query_posts’?
- How to add 2 posts under another post? Formatting should be intact
- Get taxonomy terms only of the WP_Query current posts
- How can I show the post thumbnail from the most recent of a certain post type, in widget?
- Random order of WP_Query results with highest meta value
- ACF page while loop breaks footer while loop
- Split loop into two columns, how to favor one side over the other
- creat filter with wp_query
- Security for data obtained from the database
- Syntax error when I try to insert my loop into an unordered list? [closed]
- Custom Pagination is Broken
- How to retrieve the data from the sever and displaying it in a page?
- Should I reset $wp_query?
- Posts are not looping through correctly
- Create Div Wrapper On 3rd Iteration Or When Query Loop Items Run Out
- Add class to every other posts using get_posts
- WordPress Custom Form – Getting Query Vars, Weird Glitch?
- Using a new WP_Query inside the loop
- Category Page Displaying all Pages
- Looping through dummy posts and showing them in the main index.php
- WordPress Ajax filter: Create two loops for different output styles?
- Return to the beginning of the results of a for loop
- How to count posts posts if they are in a separate content page?
- Thumbnail wouldn’t scale to size properly
- Pagination in WP Queries
- Retrieving specific images from Media Library
- direct query to post_meta table
- Adding unique marketing messages between certain products whilst using isotope/Infinite Scroll
- List sibling pages widget, exclude current page
- Implement “No related posts for this entry” into the loop?
- Options.php loop won’t show!
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise