Should be add_action not add_filter when using pre_get_posts
add_action( 'pre_get_posts', 'thefunction');
function thefunction($query) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It's the main query for a front end page of your site.
// Let's change the query using arguments.
$query->set( 'orderby', 'rand' );
}
return $query;
}
Related Posts:
- Removing any and all inline styles from the_content()
- How to exclude latest x posts from a paginated query?
- WordPress Number of Posts Not Changing With posts_per_page
- Exclude category on blog list page
- Post content being duplicated by the_content();
- Replace Main Query With Custom Query
- Custom Order in WP Query
- If specific user role then sticky post
- How can I remove posts of a certain category from homepage after a specified time period?
- pre_get_posts query between 2 dates (date stored in custom post meta)
- how to get posts ids inside pre_get_posts filter?
- Modify Image Source With The_Content Filter?
- Change post order random through out the entire WordPress
- How to exclude latest x posts from a paginated query?
- How to filter my search in post if contains a word in title, content or excerpt?
- the wp_post_update isn’t working all the time
- Display/Filter post using if else statement
- long-title posts do not want published
- Override wp_link_pages pagebreak with filter
- Add review box by function at top or bottom of content
- Get results from the main wp_query
- the_content() not displaying the full post
- wp_query posts sorting doesn’t work
- WP_query sort by custom meta_key “price”
- How add class the_content();?
- Add custom PHP (no-SQL) filter to WP_query
- Filters do not work when there are multiple (one works)
- More then one menu items are assigned with “current-menu-item” class
- How to return another post than requested
- Update post_content everytime a custom post is opened in backend
- Pausing and Resuming WP_Query results
- How to do set post permalinks using 6 digit random unique function?
- Select only post id and meta value with WP_Query
- Lost draft under all posts and drafts
- Get user categories with most posts in it
- Applying posts_clauses filter to specific queries only
- Posts and Attachments with “Published” status in Search
- Random ajax load only works with posts_per_page set to -1
- How to : pagination in 3 different custom loops on the same page
- get query() without post content?
- How safe is renovating $wp_query when doing WP_Query pagination in Page Template
- How to show full post?
- Exclude the first ‘n’ number of posts of a tag from home page?
- Query posts and return XML
- how to change the number of posts returned in a specific loop without making it global?
- Query Posts With Over 1000 Views
- How to allow visitors to filter posts by multiple taxonomies
- posts_per_page option limits the number of Gallery items
- List authors with the last post title and order by last post date
- How to order posts by meta_value and title
- Filter only the text in the_content
- wp_query random post
- wp_posts table: safely remove unused columns to save database storage
- Query Posts by Custom Field
- Adding a blog archive with pagination using WP_Query
- How to get Tags with specific post id
- Redirect to another page using contact form 7? [closed]
- how can i change WP main archives loop to sort by name or title
- Trying to alter the post_content through the_post
- Using wp_list_pages() after calling query_posts()
- how to get post order by post id wp_query?
- Possible to alternate between two loops
- Nested WP_Query breaking loop
- How to check if single.php has already called the_post_thumbnail function
- WordPress query_posts by tag doesn’t work anymore(?)
- add to end of post in the loop with plugin
- Add filter to the end of the post
- How to display all posts divided/ordered by post date? [closed]
- Why Query is returning empty array?
- How do I extract just the post ID of the first item in whatever WP_Query returns?
- Show post content and title in diferent divs using WP_Query using a loop
- Get post content with all filters applied, knowing post id
- Why are my paginated posts always returning the same results?
- Ignore latest two posts
- Is it possible to use WP_Query to only pull posts with attachments?
- Adding bootstrap classes to video shortcodes
- Is it possible to filter the main loop to exclude posts from a specific category?
- Show posts by author of membership level (Paid Membership Pro)
- Inner join overrides Advanced Custom Fields plugin’s get_field [closed]
- meta_value timestamp older than now
- Every second post different class in blog view
- Loop doesn’t exclude the specified category in home page
- Get posts with multiple categories
- Do not show children of a category
- query post limits
- post__in not working with pre_get_posts, but post__not_in does work
- Display posts of specific category term
- Print out last 3 blogposts
- why the same code got different results when using query_posts in functions.php and index.php
- Query posts by Author and/or by Tag
- Query for first 3 posts to change the look and feel
- How to get posts published on the latest date?
- Why posts array is empty?
- How to disable the_excerpt from one post
- Can’t understand why sometimes a [caption] field appears
- Page and post loop same template
- Modify posts listing at back end
- Filtering posts on Post Administration Page by Week Number instead of by Month
- Prevent posts with certain post_meta to be edited
- Getting blog pagination to work on page set as front page