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 to show correct td of table tags in wp_query loop
- How could I prevent using the same custom loop in a template file when I only need to change one meta_query parameter?
- WP_Query() load selected post
- echo var into wp_query
- How to get post category list as select in front-end?
- Querying for specific tags
- Order a WP_Query by meta value where the value is an array
- How to get thumbnail with pure PHP in a WordPress database?
- WordPress call post-ID in jquery
- How do I check if there is a tag in the title
- Why my query does not run with prepare
- How to Generate a list of Most Commented post?
- How to pick “full/thumbnail” images in the loop?
- foreach loop inside the loop creating duplicates in output
- Create Div Wrapper On 3rd Iteration Or When Query Loop Items Run Out
- mySQL queries are executed twice on wordpress website
- How to Fix an Archive.php That Displays All Posts?
- WordPress loop is not working
- posts_nav_link pulling in multiple post types
- Displaying Event within two given time frames
- List of Events with Multiple Dates: Only NEXT Date
- Remove Post if Advanced Custom Field is checked to fix paging
- Undefined variable: row?
- List all anchor links on a page
- Php code error in wordpress if else statements [closed]
- First post article different on Archives template
- How to add title attribute to archive items
- How to add ASCII symbol after each recent post [closed]
- I am officially missing something about transient posts
- Toolbar Hidden in a Virtual Page
- pagination broken – clicking next displays “page not found”
- displaying a fall back query if there’s nothing in the post-type category
- Strip from or something better?
- How to break up php code to avoid echo
- WordPress SQL JOIN query