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
- Is it posible to multiple entry value before submit Gravity forms?
- How to get Previous Visited Page Information
- Global theme customization multisite
- Select input in metabox not updated
- How to add Custom Css within the Editor?
- Woocommerce custom archive page custom content and shortcode
- optimal taxonomy or custom post types
- Page flickers in customization mode
- export and import wordpress customization
- Custom field not editable by the user
- Will WordPress updates remove Gutenberg Additional Classes?
- WordPress category custom title
- WP_Query – sort the results by meta value custom attribute wocoomerce
- WordPress search input in database, to edit information via form and update the database
- Embed Latest Track (Dynamic Updating) SoundCloud
- How to create a custom post-new.php page for plugin , no wp menu
- Trying to update Woocommerce meta values
- Multisite with subdirectories, and a WordPress installation in a custom folder
- wordpress custom fields loop
- Custom CSS Nav Menu Style Not Being Applied
- custom email validation using javascript api with elementor pro and code snippets
- javascript file not working in a plugin template file
- search form for custom fields
- display text generated via custom get query string
- Search for custom field input of a custom post type in ajax live search
- Custom admin menu items
- If Term Name Exists, Append a Unique ID to Slug
- Easily add custom HTML outside the main container?
- How can Use custom input field in functions.php page?
- We are facing this error in header unable to understand the issue
- customer specific email attachment
- When is the wp-settings-[time] cookie generated?
- How do I run a web app in my WordPress theme without the “wp-content/theme/…/webapp” in URL?
- Get All Results From Other Users That Share The Same Custom User Meta As Current User