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
- Should I delete the default themes?
- Moving WP install from local to live, what about wp_posts GUID?
- Rename UPLOADS folder with custom WP_CONTENT_DIR
- Upgrading WordPress and Plugins; any Tips to Avoid Trouble?
- Modify column_author in WP_Comments_List_Table
- What are the ways to use WordPress as a user generated content site
- Win7 Dev Environment
- Converting primary keys from autoincrement integers to UUIDs
- Adding Custom Link anchors in the top menu that points to specific sections of the site home page
- TinyMCE default link target to “_blank”
- setting a specific home page for logged in users
- Page with several user editable text content
- Fast way to add countries as a custom taxonomy term?
- trying to modify colormag-fr_FR.po
- Programmatically add a custom page/path/url/route to WordPress
- Get multiple db prefix with $wpdb
- How do I remove the eye icon that shows visibility on login screen and reset password screen
- Passing page url into a custom field via the Press-This bookmarklet
- How to extend nav-menu-item-control data in Appearance > Customize?
- Video to stretch across the entire homepage [closed]
- How can I allow access to multiple users, using the same login, at the same time?
- Does all editing of WordPress occur within the dashboard, or can we use our own text editor?
- Multiple Category Search
- Is it possible to have one page with multiple items, or 2 blogs on a site?
- Only show search results with if current date is between two dates?
- Function to allow “Anyone can register”?
- Fatal error in wp-admin/customize.php after fresh install due to null $wp_customize
- TwentyFifteen: How can it show a different color on every post page?
- Custom search results page not working with empty search
- changing font size and font of a custom message added after comment
- wp_query get post custom taxonomy in search.php
- Custom login with external provider iframe and data object
- How to apply filters if editing specific template
- How to use standalone Gutenberg (block editor) instance on any my own admin page in WordPress