You can search post by modify the query using pre_get_post
filter. add this code inside your functions.php
file
add_filter('pre_get_posts', 'search_by_category');
function search_by_category($wp_query)
{
if (isset($_GET['cat']) && !is_admin()) {
$wp_query->set('category__in', array($_GET['cat']));
}
}
Before using any query variable you must have to register/add using add_query_var.
function add_category_query_string() {
global $wp;
$wp->add_query_var('cat');
}
add_filter('init', 'add_category_query_string');
let me know if it works for you .
Related Posts:
- Custom Query to search through categories
- get_the_category and echo out link to child-most/deepest category
- Show Posts via cat+cat in URL that are in both Categories doesn’t work
- WordPress Multiple Category Search
- is_category() in pre_get_posts strange error
- Exclude categories from search query
- Rearranging posts based on categories
- How to search for categories and/or tags?
- Sticky posts on home page, search, tag and archives without plugin
- Create product category and keyword search form in woocommerce? [closed]
- How to query all posts of a specific category, and any other category combination
- is_category in pre_get_posts results in php notices
- Query the Loop without breaking it
- How to show empty category in admin menus search
- How to Check if a Child Category is Being Queried
- How to select product category while adding new product in woocommerce? [closed]
- How do I get the total number of categories in a list of search results?
- Category Search functionality on category metabox (wordpress admin)
- Search by word, category, tag, author
- Filtering search results
- How Do I Merge Categories With phpMyAdmin
- Category archive page – loop through posts of certain tag (with pagination) – pre_get_posts
- Help with multiple dropdown tags search
- If newest post of category is newest post in general, skip first post of category
- Disallow categories from this MySQL query
- Include posts from feature category in pre_get_posts
- Multiple Category Search
- How to display posts under a specific subcategory of a given category
- I need to create a search form that will display search results from specific category
- Search Posts / Pages with multiple options?
- How to query post like normal search would do. within search.php page
- query in category.php repeats itself
- Display posts separated by Category in Author’s page
- WordPress search only showing a few posts
- Site search needs to find posts for category topic AND Tag or keyword thats not in the post
- Search in custom post type AND in pages with category
- Category param redirect to post type archive
- Select posts by name and category per REST API
- How to get related tags based on a category?
- How do I toggle pagination on/off in search results and category listings via a link?
- Show popular post by category code
- How to get latest posts from each category in regular order
- Refine/Filter the Search Results by Category
- category pagination got broken suddenly
- Show matching categories in search page
- How to query categories with Advanced Custom Fields?
- Where and when does WordPress invoke routes
- Keyword search in multiple categories
- And/Or Category Query
- Get URL Category Name By ID
- Exclude category from query
- Overriding default calendar to show posts from a category
- function query_posts disabling current_page_menu class
- Group search results by category
- Woocommerce search form with category select
- Using if statement in index.php instead of creating separate template files
- How Do I Delete WordPress Posts Older Than 400 Days, From A WordPress Category
- Using in ‘category_name’ in ‘$query->set();’?
- Search form options group categories
- custom query for both category and pages using global $wpdb;
- Get categories of queried posts
- Query from multiple category (and)
- How to display posts from specific category using get_option function?
- SQL query to get posts from multiple categories but not in a category
- Conditional category query breaking?
- How to filter post from categories only if the posts appears in one of them, not the other?
- Category name for all posts getting assigned to a single random post
- WordPress Default Category and Custom Taxonomy Selected Attribute not Working After Searched in wp_dropdown_categories Array
- Automatically adding tags and categories into Post Excerpt for searching
- List of post categories only associated to another custom taxonomy
- Return all Tags and Categories in Separate List
- how tho change number of posts in loop from specific categories
- Get category from slug list
- Search Replace Database ONLY for posts of certain category?
- Search by tag, category and author without plugin
- Querying posts from two different categories while looping inside another loop
- get_terms sort order with child categories of varying depth
- how to get postmeta info based on multiple categories?
- Too many categories drastically slow down my website
- How to get posts from many categories using WP_Query
- Is it possible to query the result of a query?
- 404 error when i try to search by category or by tag
- Make related posts display first in search result
- A search box for all sites – Multisite
- show posts found in multiple categories via category_name query string?
- Match two taxonomies to display a specific content
- Several loop in search result
- Category slug in in loop always the same?
- display certain category on custom template page
- Detecting top parent category fails on home page
- How to use Greek characters/letters in a query?
- Displaying posts by year
- wordpress category.php query for featured news item, broken pagination and repeating posts
- Search functionality with multiple categories
- Query posts that have all the specified tags
- Display category tree of a post in search results
- How to show posts of a specific category
- Remove “All categories” from searchbar dropdown
- Advanced Search – Is this possible?
- Display different sidebar based on search results from one category