Run first query las 10 posts, than use array_rand
PHP function on posts array:
$args = array(
'posts_per_page' => 10,
'post__not_in' =>array( $post->ID ),
'orderby' => 'post_date',
'order' => 'DESC'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
if ( $query->post_count > 4 ) {
$query->posts = array_rand( $query->posts, 4 ); // get 4 random posts
$query->post_count = 4; // update post count
// for pagination, if you need it
$query->max_num_pages = (int) ceil( $query->found_posts / 4 );
}
// After that loop as usual
while ( $query->have_posts() ) : $query->the_post();
// loop here
endwhile;
wp_reset_postdata();
}
Related Posts:
- WP_Query for WooCommerce Products
- How can I display recent posts from a particular category in my header?
- Using AJAX to return search form results
- Pagination is not working using WP_Query
- Two queries on the same page with pagination
- Array Chunks and Inserting Varying HTML in WP_Query
- Why is this coming back as null? Thats wrong. There is one post
- How to display future posts – modified query still yields 404
- WP_Query arguments to fetch custom post type posts which are in certain category?
- Which method is faster to get a single post?
- Import wp users via one click demo option
- How can I use $wp_query->tax_query as tax_query parameter for a new WP_Query instatiation?
- Adding column to wp_posts table or join wp_posts table with another table
- Two loops on archive page
- Check if loop has any categories?
- How to make search for posts using get method?
- Reset WordPress Post Query to default
- What happens to the default query when I use WP_Query?
- How to Create a Random List of Child Pages
- Wp_query with 2 meta keys and array of meta values
- Problem with get_page_by_path() using with WP_Query
- WP_QUERY wrong ammount of posts
- WP_Query to get post on frontpage
- How to create Page templates for showing Blog posts in different layouts?
- Writing less unnecessary code with WordPress
- How to separate two columns from one content on a page template?
- Using Ajax and WP_Query to load more posts on category page fails
- WooCommerce | AJAX | Product Pagination | Help me implement Ajax Pagination
- Custom WordPress post query for displaying time-released content on website
- How to display author meta and count numbers for each user in comment.php?
- Displaying Pages in Nav Sub-Menu with Specific Taxonomy Terms?
- Global page ID variable empty error
- Get posts by similar names and categories
- Second WP_Query loop shows data from main query
- active link for most recent post on vertical tabs
- Custom Theme Building & Permalinks
- WP_Query issues with argument posts_per_page
- Get author meta data with no published posts in author.php?
- How to display posts from a specific term first then another posts from another terms using tax_query
- Why ignore_sticky_posts argument is in sticky post query?
- Way to include posts both with & without certain meta_key in args for wp_query?
- How can I use WP-CLI commands without –allow-root
- Calling a wp_query $posts causes a 500 error
- WP_query taxonomy + get all posts with two terms from same taxonomy
- How do I create my own nested meta_query using posts_where / posts_join?
- meta_query not working properly
- Best Practice For Querying Grandchildren?
- Using tax_query reverses my post_type argument in a custom WP_Query
- creating wp query with posts with specific category
- $wp_query->current_post restarts from zero on paged pages
- wp_get_post_terms of cpt but not duplicates if they are used more than once in the loop
- How to prevent queried posts from being added to cache?
- How to make orderby ‘meta_value_num’ OPTIONAL?
- How to do a query on custom taxonomies that is uncategorised?
- How can I query posts with newly uploaded images?
- Conditional arguments WP_Query for post custom fields
- Random loop with code to prevent duplicate output returns no output at all from time to time
- Single meta_query query using OR instead of AND in request’s WHERE statement
- How to choose between hooking into per_get_posts or into parse_query
- Woocommerce returns Product post_status as published even tho it is in status draft
- remove query arg from url after set query
- How can I fix: “Notice: Undefined offset: 0”?
- SQL: What is wrong with the following query (generated by WordPress WP_Query, ordering prices)
- Order By table field comment_status in WordPress > 4.0
- Slow WP_Query for custom post type
- WP_Query Pagination on multiple-loop page breaks WP or doesn’t show up
- Using hook to use DISTINCT in a wp_query
- Overwrite YoastSEO meta-tags with another page’s [closed]
- How to restrict the search fields for a specific post type
- Slow queries on a huge database
- Query with relation and one without relation using multiple taxonomies?
- WP Query – Post Thumbnail
- Search posts missing a particular custom field
- Sort by presence of thumbnail
- Link Author URL’s based on a custom field
- embedding shortcodes in php template
- wp-query, pull children of parent page
- How to change tag based on metabox value within $wp_query
- What’s missing in this wp_query and meta_query
- Tax Query on product_cat using NOT IN as operator does not exclude that category
- How to use the Term Object from a custom select field in a query
- Random users always showing same 8 users
- whether a nonce is required for get type and get_query_var?
- Wp-query and column blocks
- How to query users by post count no less than 10
- Display 3 levels of categories on page
- Showing most popular post of week
- Related Post by Tags Code
- Display three sequential posts on each page load, without repeating previous
- Find by post_meta, then sort by post_meta, then sort by date (wp_posts)
- Optimising amount of calls to custom fields
- The sorting of posts by a meta_query with two keys fails while separated as single queries it works
- Hierarchical List Pages as a table
- WP_Query with several meta_query-statements and order by meta_value
- Second wp_query doesn’t appear to be working
- difference between methods of query
- post__in not recognizing multiple IDs
- Related posts with WP_Query
- Get posts after calculating meta key
- Display Featured image from custom post type category (custom taxonomy) wise