Search Using Post ID

I previously asked and answered my own question about customizing a search. You can find that answer here: The right way to create a custom search page for complex custom post types. It’ll be a good read to help you understand the following code. I’ll use the pre_get_posts action to change your search when you … Read more

Exclude categories from search query

You can use pre_get_posts action to exclude categories from search query. function wcs_exclude_category_search( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_search ) { $query->set( ‘cat’, ‘-22, -21’ ); } } add_action( ‘pre_get_posts’, ‘wcs_exclude_category_search’, 1 ); You should paste this code in your theme’s functions.php file.

Have different search results template depending of custom post type searched

So I have found the solution, I leave it here if anyone needs something like this in the future. On your search form add a hidden field: <input type=”hidden” name=”post_type” value=”post_type_name” /> Then on your search.php file add the following WITHIN the loop: <?php if(isset($_GET[‘post_type’])) { $type = $_GET[‘post_type’]; if($type == ‘post_type_name_1’) {?> <!–Your Code … Read more

wp_list_table search box does not show

You must use $list_obj->prepare_items() before $list_obj->search_box(‘Search’, ‘search’) function! Example: <form method=”post”> <input type=”hidden” name=”page” value=”<?php echo $_REQUEST[‘page’]; ?>” /> <?php $this->sales_obj->prepare_items(); $this->sales_obj->search_box(‘Search’, ‘search’); $this->sales_obj->display(); ?> </form>

Disable Redirect to Product Page on Search Results Page in WooCommerce [closed]

Yes, this is totally possible. WooCommerce includes a handy filter that you can manipulate to disable that redirect so the user will continue onto the search results page as per normal. If you add this line of code to your functions.php file it will disable the redirect functionality. add_filter( ‘woocommerce_redirect_single_search_result’, ‘__return_false’ ); Permalink to gist: … Read more

Do I need to sanitize WordPress search query?

No. WordPress sanitizes the search query. To use the sanitized search query, use the_search_query() to echo, or get_search_query() to return, the search query. Edit Based on your edit: Don’t use $_GET[‘s’]. Use get_search_query(). Don’t use $_GET[‘cat’]. Use get_the_category(). All $_GET and $_POST data should be assumed to be inherently unsafe, and should be sanitized/validated accordingly.

How to pass a search $_GET parameter to a new custom search page?

Problem is that ‘s’ param is a standard query param of WordPress, when you use the url /custom-search?s=mysearchstring your are saying to WordPress to retrieve the page ‘custom-search’ that contain the string ‘mysearchstring’ and this bring a 404. You have 2 possibilities: use another query string name, something like /custom-search?cs=mysearchstring then inside the page template … Read more