How to add WooCommerce SKU to search query?

If you are using wordpress search you can add this code to make it work function search_by_sku( $search, &$query_vars ) { global $wpdb; if(isset($query_vars->query[‘s’]) && !empty($query_vars->query[‘s’])){ $args = array( ‘posts_per_page’ => -1, ‘post_type’ => ‘product’, ‘meta_query’ => array( array( ‘key’ => ‘_sku’, ‘value’ => $query_vars->query[‘s’], ‘compare’ => ‘LIKE’ ) ) ); $posts = get_posts($args); if(empty($posts)) … Read more

Changing the search results destination URL

You can create a new page. Let’s say you want to have: http://example.com/mysearch/ Create a page that will have that URL structure. Next, Search form -> Go to and on you search form action do: <form role=”filter-search” method=”get” id=”sd_searchform_filter” action=”<?php echo home_url( ‘/mysearch/’ ); ?>”>… Now go to functions.php (or where you want this function) … Read more

Conditional tag search-no-results

There is no conditional tag for no results on a search page, but you can create yourself one. You basically just have to check the value of $wp_query->found_posts, if it is 0, returns false, any other value, returns true function is_search_has_results() { return 0 != $GLOBALS[‘wp_query’]->found_posts; }

Set default text for the_search_query();

Do not hard code a value in the search field. You will get a lot of nonsense searches because people do not expect that they have to clean up the field before they can actually type something into it. They will just type and hit enter. Alternatives: Use the placeholder attribute. You can style it … Read more

Separating search results collectively by type

There’s two options. The first and probably the easiest option is to run the query twice on the page in each place you want to use it. // Author Block while(have_posts()) : the_post(); if ( ‘book-author’ == get_post_type() ) { ?> <h2>AUTHOR</h2> <?php the_title(); } endwhile; rewind_posts(); // Book Block while(have_posts()) : the_post(); if ( … Read more

Get the selected term in a dropdown search

To show dropdown with currently selected option, first you need to fetch current value. You can get that value from $_REQUEST variable. If you have grade variable then your current value is $_REQUEST[‘grade’]. Eg. if ($_REQUEST[‘grade’] == $term->slug) Recommended: Do not use REQUEST variable directly without sanitization. Use function like esc_attr. If you are sure … Read more

WordPress Single Conditional for Search Page

You could hook into the the_permalink filter and check for is_search() to conditionally modify the URLs of your search results to include some additional parameters that your single.php then checks for. Something like this (may require tinkering): add_filter( ‘the_permalink’, ‘wpse155331_the_permalink’ ); function wpse155331_the_permalink( $url ) { if ( is_search() ) { $url = add_query_arg( array( … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)