When you search on wp it automatically does a search query. In your code you make a new WP_Query to show 5 posts (which sorts by data by default) and there is no need to do that.
The best thing to do here is to leave the search.php
file as it is and use an action hook. You can add this to your child theme functions.php file.
function my_search_filter( $query ) {
if ( $query->is_main_query() ) {
if ( $query->is_search ) {
$query->set('posts_per_page', '5');
}
}
}
add_action('pre_get_posts','my_search_filter');
Related Posts:
- GET the excerpt by ID
- Change Posts per page count
- How to end the excerpt with a sentence rather than a word?
- The next_posts_link() works only with original $wp_query
- Allow variable amount of comments before pagination
- Skipping first 3 posts in wp query
- How to load paginated post pages via ajax
- How to disable content pagination?
- Modify ‘Read more’ link adding a new class
- How to Group Posts by the First Letter or Number?
- Remove image caption from post excerpt
- How can I display 7 posts on the home page, but 9 posts on the subsequent pages?
- Custom Post Type Pagination Doesn’t Work in WordPress 3.4
- Automatically create pages in a post based on number of words
- Setting pagination for images attached to a post
- Pagination not working on Custom Page Template
- Show number of posts AND number on current page
- Pagination for Pages and Posts
- Programmatically inserting page breaks
- I’m having some issues with previous_post_link() and next_post_link()
- How to add an admin function only to posts, not pages?
- How to customize read more link
- Custom wp_trim_words() function not trimming right
- Different amount of posts on homepage than paged pages
- JetPack Infinite Scroll not working on custom theme
- Pagination – Posting First Page Content
- Load posts into sidebar and paginate via ajax?
- How can I get all posts data from within a paginated search result?
- How to show post(excerpt) from specific category on wp page?
- How to link up “read more” on excerpts hack from WP Recipies
- When paginating a Page with the tag, how can the 2nd and subsequent page styles be customised?
- Append Text to $post->post_excerpt
- Frontend Post Excerpt field mapping
- Allowing the SoundCloud player to appear in a post excerpt
- Auto Refresh Post List after X seconds
- How to show video from specific category on sidebar?
- Related post not showing table of content
- Search results with pagination not working
- How to limit wordpress the_excerpt() dynamically
- Go back to the exact post page number
- Problem paginating a subquery on a post
- Pagination Not Working With Custom Post Type Category
- Pagination Stops
- Numbered Pagination Showing The Same Posts After Altering WP_Query
- Remove pagination if search returns empty
- Moved a website and excerpts don’t work for old posts
- Enable HTML Tags in WordPress Post excerpt
- How to display the pagination in my post list? [duplicate]
- How can I get the last post while on the first post for pagination? As if it were infinite
- Disable pagination in posts and pages
- Pagination (on the static front page) shows always the same posts
- Code to display random ordered posts in Categories across pages [duplicate]
- Count posts for pagination
- Override wp_link_pages pagebreak with filter
- How to display post excerpt beside post list inside Post of Dashboard
- Can I Paginate Post after every 25 tag?
- Pagination for single post
- Posts are not showing up on next page.
- Trim excerpt to first paragraph
- Nothing appears using get_the_excerpt() in category.php
- Output wp_link_pages as raw url
- Admin is showing all posts on page 1
- Custom Select Query pagination not working properly
- Post List Widget with custom posts and editing the Read More Link
- Thumbnail With Next/Prev Links Not Showing Next Post?
- Show category post with excerpt text
- Post of a specific category – not to be seen in paging previous/next post
- Add wp-link-pages to post
- wordpress paginate, link to the right page
- Pagination working locally but not on live site
- Pagination and Related Posts
- Pagination for custom query won’t let me back on page 1
- Make a paginated article (for easier reading on mobile)
- Add a minimum word count to Excerpt (and how to force Excerpt as a rule for WyPiekacz)
- how to show posts on different pages if number of post per page is one
- How do I paginate posts page with $wpdb?
- Can’t get pagination to work
- How to display a paginated list of posts from one category on a page? [duplicate]
- How to inject a post within a loop
- The next_posts_link() works only with original $wp_query
- How can i show only 30% of the “the_content(“More…”)”?
- how to add pagination to the organize series plugin
- Custom Post Type Pagination 404 Error (if paged>=2) [duplicate]
- Not displaying any articles on a custom made file
- Using the_excerpt() some posts are truncated and some are not
- WordPress posts loop pagination – first page return 125 posts instead of 10 and the rest return 10
- Issue with wordpress pagination: last 2 posts show again on second page
- Limit amount of pages shown in pagination within wp_query
- How to show only the date, the title and a little “summary” of my WordPress post in my custom theme?
- How to get pagination working with “Pending” Posts
- Page not found after clicking on pagination
- wp trim function not working
- Single post pagination
- WordPress Custom Theme: My site shows the same posts on Page 1 and Page 2 and so on [duplicate]
- excerpt in template for specific page
- query_posts different amount of posts per page [duplicate]
- Show full post only to loged-in users
- Read more opens attachment
- Show a page counter for pagination
- Parameter for pagination posts_pagination