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)
- How to get ‘post_content’ without stripping tags?
- How to Change Order of Posts in Admin?
- The next_posts_link() works only with original $wp_query
- Query *only* sticky posts
- How to disable content pagination?
- Accessing the post content with WP_Query
- Adding a div class or id inside the_content()
- how to display post content without post image?
- How to remove_filter that filters iframes in posts? [duplicate]
- Wp-query causing problems with the_content();
- How to output comments number of a post per day?
- How to show video from specific category on sidebar?
- Display posts with comments closed, with pagination?
- posts_per_page doesnt work
- the_posts filter been called multiple time
- Only display posts after current date
- I want to query posts in monthly basis
- Custom WP_QUERY $args
- Limiting the number of posts in WP_Query leads to unexpected result
- Ajax posts filter by date, comments, top views, top likes
- how to remove dash (-) post status from post title on posts listing page wordpress
- Get latest posts from WordPress site without header, menu and sidebar
- How to add pagination in between post and comments?
- Why WP_Query(‘showposts=5’) shows only 1 post?
- How to hide html tags on revision comparison pages?
- More then one menu items are assigned with “current-menu-item” class
- How to do set post permalinks using 6 digit random unique function?
- get query() without post content?
- how to change the number of posts returned in a specific loop without making it global?
- posts_per_page option limits the number of Gallery items
- Redirect to another page using contact form 7? [closed]
- Nested WP_Query breaking loop
- add to end of post in the loop with plugin
- Adding bootstrap classes to video shortcodes
- Get posts with multiple categories
- Do not show children of a category
- post__in not working with pre_get_posts, but post__not_in does work
- Display current post position in Elementor Posts widget
- How to give classname to post if post has no content?
- Custom wp-query in wordpress rest api
- posts_per_page returning only one post
- Need to know custom code to display random and most viewed posts in wordpress posts and pages
- How to retrieve certain number of images from a wordpress post?
- WordPress post filter menu
- Not all posts showing in query
- WP_Query custom field pass the post id
- WordPress query portfolio posts
- Current and next month posts
- Same post appears in related Posts?
- Trying to exclude custom posts based on date, while sorting by custom field
- How to show multiple posts thumbnail, title and date in widget
- Show only posts with titles/permalinks that do not contain certain words
- Group first, 2nd, 3rd, etc posts by category terms
- Manipulated offset and pagination, can’t display last post anymore
- Filter posts by month (dropdown)
- How to limit wordpress the_excerpt() dynamically
- How to display particular set of wordpress post on a webpage?
- Add view to admin menu to filter for specific criteria ( If post is child of specific Parent )
- show loop id post , only first id can read
- Add id attribute to h1 element of wordpress post
- Show 5 posts and than 3 posts offset with pagination
- Insert wp_query after the_content with plugin (filter the_content won’t work)
- Is it possible to query a category with specified posts?
- WordPress loop uses unmodified posts array, why?
- Show posts between two Custom Fields date
- Query to get data of a post, if in category?
- Order posts by custom field
- Why I have this strange behavior when I try to exlude the featured post from posts visualization?
- Remove duplicated posts in the loop if post has more than one category
- Why I getting only 1 post?
- Stripping tag from elements in post
- First post in loop displays twice
- WP_Query post_tilte search in posts table