I heard query_posts() is bad so I shouldn’t be using that, but how
else could I achieve this?
Yes, query_posts
is bad. You heard right. You are most of the way to doing this already. You have all the parts. You just need to mix them a little.
function searchfilter($query) {
if ($query->is_search()) {
$query->set('posts_per_page', 5);
if(isset($_GET['search-type'])) {
$type = $_GET['search-type'];
if($type == 'news') {
$query->set('category_name','news'); // needs to be the slug not the name
}
}
}
return $query;
}
add_filter('pre_get_posts', 'searchfilter');
Related Posts:
- Custom search to display results within same page
- Custom dropdown search form that allows users to select option B based from Option 1
- Creating my own Admin Forms in a WordPress CMS?
- Creating search form for custom database table
- Search through custom post type and custom fields takes 5 minutes
- Customise search form in the Widget
- Comment filtering (search)
- Combine multiple words in custom meta search query
- Custom search page and search by title, content and tag
- Using transients to store captchas
- How to implement a custom password field that redirects to another page upon correct password?
- Add Div to Comment Form
- Search a post using the date it was posted
- Setting custom search engine indexing for a “dynamic WordPress page” with htaccess
- Custom URL going to 404
- Search result page redirect ex.com/?s=searchterms to ex.com/searchterms
- Hi, I wanted to make custom search with multiple fields
- Adding custom field in all widgets, but at the top of the form, in admin area
- Steps for custom data retrieval
- Search widget will search everything in the site, how to limit to only search gallery name
- How Can I Move Data From Form 1 To Form 2
- How to hook up search results page to a back-end page?
- Admin search post meta
- Add a searchbox to the body of a page
- Custom Search on Custom Page while preserving the main search function
- Custom Loop Query For Search Page
- Search URL parameters query [duplicate]
- Applying WP-cli Search & Replace to Static SQL Dump File
- Saving contact form 7 data into custom Table
- How to create custom search page
- Woocommerce custom checkout form
- Custom login and registration forms
- Custom Same Page Search
- Custom Form not generating URL
- How to Add Additional Search Button?
- How to Clear Empty Search values?
- How do I make Dynamic Student Admission Form?
- WP Search Form Query: Add Author’s posts into search query
- WordPress search on a custom table
- How to create multipage form and redirect to specific URL based on inpput?
- No plugin populate user information in to form
- Adding overlay search to wordpress using add action/filter
- How do I add a dropdown menu to a form?
- Redirecting wordpress search query to archive page
- Allow WP users to sort search result on a videos site
- WP_User_Query Search Multiple Keywords from a Multi-Select Field?
- How to automatically pull an information from a form field onto an other page?
- How to submit form data in the same page in WordPress without reloading the page?
- Custom search results
- How to get name and email value from a custom form and add it to campaign monitor subscriber list?
- WordPress custom search – pagination
- Creating a search with tag variable also added for more detailed results
- What shortcode should I use for
- Custom user fields validation on registration
- Search a meta field for a value in all posts
- add unique code required to register
- Custom search by several options send on form not works
- Page template with custom html fields inside a content
- Search results load individual post in slide out div
- Custom Search Results Page
- WordPress Search on Multiple wp sites
- Create a custom search for Estate Agency WordPress site
- How can I make a search term redirect to a page
- Sort Search Results in Order of/by Character @ count (9) of post(s) title. (Ocean-WP theme)
- Show total sales for each products on search results page Woocommerce
- Custom Search on media files PDF images pages posts
- WordPress hook before inserting post into database
- Enable / Add Custom Keyboard Shortcuts To Work With WordPress’ HTML Editor
- How do I add Post Like and Bookmark Functionality to my WordPress Site?
- How to exclude certain widget from showing up on home/front page? [duplicate]
- What is causing this error? “Warning: Invalid argument”
- Modifying loop on a custom page, strange behaviour
- WordPress native “playlist” shortcode. Next and Prev there are but with no icons. How to fix?
- Show post page only if the user has bought a specific product
- Create simple Cookie Box for WordPress
- Does wordpress prevent non wordpress pages from being allowed on the same domain?
- Creating a custom search form
- Different development environments (Mac & Windows)
- Is there a way to set the value and text for Select/Radio/Checkbox using More Fields?
- the_post_thumbnail() returns nothing even though has_post_thumbnail() returns true
- orderby in query_posts
- Problem with custom user meta field
- What are “best practices” for wp-config.php
- Instant install of wordpress
- In WordPress 3.x, I can’t access posts from all blogs in the network
- WordPress Install Issue, Receive Warnings But Successful Install
- Create Miltiple Category Base
- search form for custom fields
- Get /users/me details from WordPress backend
- How to show the hero of website when sending website link?
- Unable to write error_log output… but only from a certain function
- WordPress integration with Backbone or AngularJS
- Special characters showing in fallback font
- Customise default site setup
- Adding a third custom taxonomies
- Storing form data into wordpress database [closed]
- Disable all Fonts that are Standard in WordPress and replacing them with a custom Font?
- Custom product sorting archive page
- Add custom information in editor summary
- A Voting system for a pageants